-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckArrEle.js
More file actions
53 lines (42 loc) · 1.41 KB
/
checkArrEle.js
File metadata and controls
53 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Create a function that returns true when two arrays
//Contains common items.
const array1 = ["a", "b", "c", "d"];
const array2 = ["x", "y", "z", "a", "b"];
//Solution 1
function containsCommonItems(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) return true;
}
}
return false;
}
// console.log(containsCommonItems(array1, array2));
//This is the brute force approach
//You may not code this but explain why it works and why it is not the best solution
//time complexity is too high for large inputs - O(n*m)
//Space complexity - O(1)
//Solution 2
function containsCommonItems2(arr1, arr2) {
// convert first array to object using for loop
map = {};
for (let i = 0; i < arr1.length; i++) {
if (!map[arr1[i]]) map[arr1[i]] = true;
}
for (let j = 0; j < arr2.length; j++) {
if (map[arr2[j]]) return true;
}
return false;
// check if items in second loop is equal to object key using for loop
}
// console.log(containsCommonItems2(array1, array2));
//This is a better solution as regards time complexity but not space complexity
//time complexity - O(n+m)
//space complexity = O(n+1) = O(n)
//Solution 3
function containsCommonItems3(arr1, arr2) {
return arr1.filter((item) => arr2.includes(item));
}
console.log(containsCommonItems3(array1, array2));
// Most advanced solution
// Using inbuilt javascript array methods