-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path349_Intersection_of_Two_Arrays.js
More file actions
38 lines (33 loc) · 1005 Bytes
/
349_Intersection_of_Two_Arrays.js
File metadata and controls
38 lines (33 loc) · 1005 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
31
32
33
34
35
36
37
38
/*
349. Intersection_of Two Arrays
Given two arrays, write a function to compute their intersection.
Note:
Each element in the result must be unique.
The result can be in any order.
*/
const expect = require('expect');
describe('349 Intersection_of Two Arrays', () => {
it('returns intersection of two arrays', () => {
//arrange
const num_1 = [1, 5, 2, 3, 4, 6, 7, 3, 2, 5, 6, 1];
const num_2 = [1, 2, 5, 7, 8, 3, 2, 6, 3, 2];
const input = [num_1, num_2];
const expected = [1, 5, 2, 3, 6, 7];
//act
const actual = intersection(...input);
//assert
//the result can be in any order
expect(actual.sort()).toEqual(expected.sort());
});
});
const intersection = (num1, num2) => {
const tempHash = {};
const returnHash = {};
num1.forEach(value => {
if (!tempHash[value]) tempHash[value] = true;
});
num2.forEach(value => {
if (tempHash[value]) returnHash[value] = true;
});
return Object.keys(returnHash).map(value => parseInt(value));
};