-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.js
More file actions
executable file
·30 lines (22 loc) · 888 Bytes
/
intersection.js
File metadata and controls
executable file
·30 lines (22 loc) · 888 Bytes
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
// Intersection of two arrays
// a fundamental set theory problem that teaches efficient intersection operations using hash sets. Master the O(1) lookup pattern that's essential for data deduplication and filtering tasks.
// Return unique elements that are common in both arrays
// example input ---> nums1 = [1,2,2,1], nums2=[2,2] | output ---> [2]
//1. store all elements of the first array in a set
//2. iterate through the second array and check if elements exist in the set
//3. Add common elements to a result Set to ensure uniqueness
// O(n + m) space O(n)
const arr1 = [1, 2, 2, 1];
const arr2 = [2, 2];
function intersection(arr1, arr2) {
let set = new Set(arr1);
console.log(set);
let result = [];
for (let num of arr2) {
if (set.has(num)) {
result.push(num);
}
}
return Array.from(new Set(result));
}
console.log(intersection(arr1, arr2));