Skip to content

Commit 275fb90

Browse files
committed
Add solution #350
1 parent 94197dc commit 275fb90

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
263|[Ugly Number](./0263-ugly-number.js)|Easy|
2222
264|[Ugly Number II](./0264-ugly-number-ii.js)|Medium|
2323
345|[Reverse Vowels of a String](./0345-reverse-vowels-of-a-string.js)|Easy|
24+
350|[Intersection of Two Arrays II](./0350-intersection-of-two-arrays-ii.js)|Easy|
2425
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
2526
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
2627
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 350. Intersection of Two Arrays II
3+
* https://leetcode.com/problems/intersection-of-two-arrays-ii/
4+
* Difficulty: Easy
5+
*
6+
* Given two integer arrays nums1 and nums2, return an array of their intersection.
7+
*
8+
* Each element in the result must appear as many times as it shows in both arrays
9+
* and you may return the result in any order.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums1
14+
* @param {number[]} nums2
15+
* @return {number[]}
16+
*/
17+
var intersect = function(nums1, nums2) {
18+
const result = [];
19+
const map = {};
20+
21+
nums1.forEach(value => map[value] = (map[value] || 0) + 1);
22+
nums2.forEach(value => {
23+
if (map[value]) {
24+
result.push(value);
25+
map[value]--;
26+
}
27+
});
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)