Skip to content

Commit c75df16

Browse files
committed
added solution to intersection of two arrays
1 parent 83b9062 commit c75df16

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Intersection of two arrays
2+
3+
| # | Difficulty | Tag(s) | Link |
4+
| --- | ---------- | ---------------------------------------------- | ------------------------------------------------------------------------- |
5+
| 40 | Easy | Hash map, two pointers, sorting, binary search | [View problem](https://leetcode.com/problems/intersection-of-two-arrays/) |
6+
7+
## Approaches
8+
9+
- hash map
10+
- two pointers
11+
- binary search
12+
13+
### Hash map
14+
15+
- we have to find the numbers that are present in both arrays
16+
- we can include in the result array only one occurrence of a common number
17+
- use a hash map to store unique numbers present in arr1
18+
- loop through arr2 and check if it contains unique numbers of arr1
19+
- whenever we find a match, we add that number to the result arr, and delete it from the hash map
20+
- O(m + n) time complexity
21+
- O(k + l) space complexity
22+
- k is the count of unique numbers in arr1
23+
- l is the size of the result array
24+
25+
### Two pointers
26+
27+
- sort the two arrays in ascending order
28+
- use a set to store common unique numbers present in both arrays
29+
- `i` is used to track numbers of arr1
30+
- `j` is used to track numbers of arr2
31+
- keep iterating as long as both i and j are within the bounds of their respective arrays
32+
- if `arr1[i] < arr2[j]`, then increment i so that it points to a bigger number in arr1
33+
- if `arr2[j] < arr1[i]`, then increment j so that it points to a bigger number in arr2
34+
- if `arr1[i] === arr2[j]`
35+
- add the number to result array
36+
- increment both i and j
37+
- O(mlogm + nlogn) time complexity
38+
- O(k + l) space complexity
39+
- k is the size of the set
40+
- l is the size of the result array
41+
42+
### Binary search
43+
44+
- sort the arrays in ascending order
45+
- use a set to store common unique numbers present in both arrays
46+
- loop through arr1 and use each number as the target when you perform binary search on arr2
47+
- if the target is present in arr2, then we add that number to the set
48+
- O(mlogm + nlogn) time complexity
49+
- O(k + l) space complexity
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Using a hash map
2+
function intersection(nums1, nums2) {
3+
const uniqueNums = {};
4+
const result = [];
5+
6+
for (let i = 0; i < nums1.length; i++) {
7+
const num = nums1[i];
8+
if (uniqueNums[num] === undefined) {
9+
uniqueNums[num] = true;
10+
}
11+
}
12+
13+
for (let i = 0; i < nums2.length; i++) {
14+
const num = nums2[i];
15+
if (uniqueNums[num] !== undefined) {
16+
result.push(num);
17+
delete uniqueNums[num];
18+
}
19+
}
20+
21+
return result;
22+
}
23+
24+
console.log(intersection([1, 2, 2, 1], [2, 2]));
25+
console.log(intersection([4, 9, 5], [9, 4, 9, 8, 4]));
26+
console.log(intersection([6, 2, 5], [1, 2, 10, 6, 11, 5]));
27+
28+
// Two pointers
29+
function intersectionV2(nums1, nums2) {
30+
nums1.sort((a, b) => a - b);
31+
nums2.sort((a, b) => a - b);
32+
33+
const uniqueNums = new Set();
34+
let i = 0;
35+
let j = 0;
36+
37+
while (i < nums1.length && j < nums2.length) {
38+
const num1 = nums1[i];
39+
const num2 = nums2[j];
40+
41+
if (num1 < num2) i++;
42+
else if (num2 < num1) j++;
43+
else {
44+
uniqueNums.add(num1);
45+
i++;
46+
j++;
47+
}
48+
}
49+
50+
return [...uniqueNums];
51+
}
52+
53+
console.log('---------');
54+
console.log(intersectionV2([1, 2, 2, 1], [2, 2]));
55+
console.log(intersectionV2([4, 9, 5], [9, 4, 9, 8, 4]));
56+
console.log(intersectionV2([6, 2, 5], [1, 2, 10, 6, 11, 5]));
57+
58+
// Binary search
59+
function intersectionV3(nums1, nums2) {
60+
nums1.sort((a, b) => a - b);
61+
nums2.sort((a, b) => a - b);
62+
63+
const uniqueNums = new Set();
64+
65+
for (let i = 0; i < nums1.length; i++) {
66+
const target = nums1[i];
67+
if (numExists(nums2, target)) uniqueNums.add(target);
68+
}
69+
70+
return [...uniqueNums];
71+
72+
function numExists(arr, target) {
73+
let start = 0;
74+
let end = arr.length - 1;
75+
76+
while (start <= end) {
77+
const middleIndex = Math.floor((end - start) / 2) + start;
78+
if (arr[middleIndex] === target) return true;
79+
else if (arr[middleIndex] < target) start = middleIndex + 1;
80+
else end = middleIndex - 1;
81+
}
82+
83+
return false;
84+
}
85+
}
86+
87+
console.log('---------');
88+
console.log(intersectionV3([1, 2, 2, 1], [2, 2]));
89+
console.log(intersectionV3([4, 9, 5], [9, 4, 9, 8, 4]));
90+
console.log(intersectionV3([6, 2, 5], [1, 2, 10, 6, 11, 5]));

0 commit comments

Comments
 (0)