Skip to content

Commit 04d82da

Browse files
committed
Intersection of Two Arrays
1 parent 9e904b7 commit 04d82da

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Intersection of Two Arrays.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*Given two arrays, write a function to compute their intersection.
2+
3+
Example:
4+
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
5+
6+
Note:
7+
Each element in the result must be unique.
8+
The result can be in any order.*/
9+
10+
/**
11+
* @param {number[]} nums1
12+
* @param {number[]} nums2
13+
* @return {number[]}
14+
*/
15+
var intersection = function(nums1, nums2) {
16+
a = new Set(nums1)
17+
b = new Set(nums2)
18+
return [...a].filter(x => b.has(x))
19+
};

0 commit comments

Comments
 (0)