Skip to content

Commit 0bcaeba

Browse files
authored
Create 350-intersection-of-two-arrays-ii.js
1 parent 6204a77 commit 0bcaeba

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

350-intersection-of-two-arrays-ii.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
const intersect = function(nums1, nums2) {
7+
const res = []
8+
const map = {}
9+
for(let i = 0; i < nums1.length; i++) {
10+
if(map.hasOwnProperty(nums1[i])) {
11+
map[nums1[i]] += 1
12+
} else {
13+
map[nums1[i]] = 1
14+
}
15+
}
16+
17+
for(let j = 0; j < nums2.length; j++) {
18+
if(map.hasOwnProperty(nums2[j]) && map[nums2[j]] > 0) {
19+
res.push(nums2[j])
20+
map[nums2[j]] -= 1
21+
}
22+
}
23+
24+
return res
25+
};

0 commit comments

Comments
 (0)