function checkSubset(arr1, arr2) {
if ((Array.isArray(arr1) && arr1.length > 0) && (Array.isArray(arr2) && arr2.length > 0)) {
const hashMap = {};
let k = 1;
arr1.forEach(elm => {
hashMap[elm] = k;
k++;
});
const isSubsetOfFirst = arr2.every(elm => hashMap.hasOwnProperty(elm));
console.log('Is Second array subset of First array:', isSubsetOfFirst);
return isSubsetOfFirst;
}
console.error('Provided parameters are not of type Array or the provided Arrays are Empty');
}
O(n)
If we calculate the time complexity of each statement of the above solution we'll get higher time complexity of
n + 1
for statements arr1.forEach()
and arr2.every()
If we ignore the constant then it'll be n
.
If we add both n
and n
it'll be 2n
.
Again if we ignore the constant the Time Complexity of the function will be
O(n)