AlgoDaily Solution
1var assert = require('assert');
2
3function intersection(nums1, nums2) {
4 const set = new Set(nums1);
5 const fileredSet = new Set(nums2.filter((n) => set.has(n)));
6 return [...fileredSet];
7}
8
9try {
10 assert.deepEqual(intersection([6, 0, 12, 10, 16], [3, 15, 18, 20, 15]), []);
11
12 console.log(
13 'PASSED: `intersection([6,0,12,10,16],[3,15,18,20,15])` should return `[]`'
14 );
15} catch (err) {
16 console.log(err);
17}
18
19try {
20 assert.deepEqual(intersection([1, 5, 2, 12, 6], [13, 10, 9, 5, 8]), [5]);
21
22 console.log(
23 'PASSED: `intersection([1,5,2,12,6],[13,10,9,5,8])` should return `[5]`'
24 );
25} catch (err) {
26 console.log(err);
27}
28
29try {
30 assertSameMembers(
31 intersection(
32 [4, 17, 4, 4, 15, 16, 17, 6, 7],
33 [15, 2, 6, 20, 17, 17, 8, 4, 5]
34 ),
35 [15, 6, 17, 4]
36 );
37
38 console.log(
39 'PASSED: `intersection([4,17,4,4,15,16,17,6,7],[15,2,6,20,17,17,8,4,5])` should return `[15,6,17,4]`'
40 );
41} catch (err) {
42 console.log(err);
43}
44
45try {
46 assert.deepEqual(intersection([3], [15]), []);
47
48 console.log('PASSED: ' + '`intersection([3],[15])` should return `[]`');
49} catch (err) {
50 console.log(err);
51}
52
53try {
54 assert.deepEqual(intersection([2, 16, 8, 9], [14, 15, 2, 20]), [2]);
55
56 console.log(
57 'PASSED: ' + '`intersection([2,16,8,9],[14,15,2,20])` should return `[2]`'
58 );
59} catch (err) {
60 console.log(err);
61}
62
63function assertSameMembers(a, b) {
64 return JSON.stringify(a.sort()) === JSON.stringify(b.sort());
65}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
65
var assert = require('assert');
function intersection(nums1, nums2) {
// fill this in
return [];
}
try {
assert.deepEqual(intersection([6, 0, 12, 10, 16], [3, 15, 18, 20, 15]), []);
console.log(
'PASSED: `intersection([6,0,12,10,16],[3,15,18,20,15])` should return `[]`'
);
} catch (err) {
console.log(err);
}
try {
assert.deepEqual(intersection([1, 5, 2, 12, 6], [13, 10, 9, 5, 8]), [5]);
console.log(
'PASSED: `intersection([1,5,2,12,6],[13,10,9,5,8])` should return `[5]`'
);
} catch (err) {
console.log(err);
}
OUTPUT
Results will appear here.