-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffTwoArrays.js
More file actions
49 lines (35 loc) · 1.02 KB
/
diffTwoArrays.js
File metadata and controls
49 lines (35 loc) · 1.02 KB
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
let log = console.log;
function diffArray(arr1, arr2) {
var newArr = [];
for (var i=0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) === -1){
newArr.push(arr1[i]);
}
}
// Looping through arr2 to find elements that do not exist in arr1
for (var j = 0; j < arr2.length; j++) {
if (arr1.indexOf(arr2[j]) === -1){
// Pushing the unique to arr2 elements to the newArr
newArr.push(arr2[j]);
}
}
return newArr;
}
console.log(
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5,"pizza"]));//[ 4, 'pizza' ]
function diffArray2(arr1, arr2) {
return arr1.concat(arr2).filter(item => !arr2.includes(item) || !arr1.includes(item));
}
console.log(
diffArray2([1,2,3,5,126], [1,2,2,3,4,"wagon"]));//[ 5, 126, 4, 'wagon' ]
function diffArray3(arr1, arr2) {
function diff(a, b) {
return a.filter(item => b.indexOf(item) === -1);
}
return [
...diff(arr1, arr2),
...diff(arr2, arr1)
]
}
console.log(
diffArray2([1,2,3,5,126], [1,2,2,3,4,"spice"]));//[ 5, 126, 4, 'spice' ]