-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path12_intersection.js
72 lines (52 loc) · 2.18 KB
/
12_intersection.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
If there are 2 arrays then their intersection means the list "only" common elements.
Eg. array1 [2,4,6,8,10,12,14,16,18,20] and array2 [4,8,12,16,20,24,28,32,36,40]
Then their intersection would include only common elements i.e 4,8,12,16,20
Includes functions -> This function takes a value/number as input and returns true if that element exists in the array and return false if doesn't exist
*/
let array1 = [2,4,6,8,10,12,14,16,18,20] ;
let array2 = [4,8,12,16,20,24,28,32,36,40];
let intersection = array1.filter(function(value){
if(array2.includes(value)){
return true;
}else{
return false;
}
});
console.log(intersection);
/*
Output
[ 4, 8, 12, 16, 20 ]
*/
// Find the intersection of the arrays inside a 2d array
let array2D = [
[10,50,70,80,90,100,30,60],
[11,50,75,85,90,100,34,60],
[10,51,70,80,90,100,30,60],
[11,50,75,65,92,100,34,60],
[10,50,70,80,90,100,30,60],
];
let res = array2D.reduce(function(previousValue,currentValue){
let intersectionOfTwoArrays = previousValue.filter(function(value){
if(currentValue.includes(value)){
return true;
}else{
return false;
}
})
return intersectionOfTwoArrays;
});
console.log(res);
/*
Here we use reduce since we need to process each array individually
For the first iteration we get 0th element of the 2d array in the previous value and 1st array in current value
Now, here 0th element is an array in itself of use a filter on it to get its intersection with 1st element.
And now we return this intersection as the previous value for the next call.
1st iteration => pv : [10,50,70,80,90,100,30,60], cv : [11,50,75,85,90,100,34,60], ==> intersection : [50,90,100,60]
2nd iteration => pv : [50,90,100,60], cv : [10,51,70,80,90,100,30,60], ==> intersection : [90,100,60]
3nd iteration => pv : [90,100,60], cv : [11,50,75,65,92,100,34,60], ==> intersection : [100,60]
4th iteration => pv : [100,60], cv : [10,50,70,80,90,100,30,60], ==> intersection : [100,60]
Hence we get the output : 100,60
Output
[ 100, 60 ]
*/