-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffArray.js
More file actions
43 lines (35 loc) · 1014 Bytes
/
diffArray.js
File metadata and controls
43 lines (35 loc) · 1014 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
39
40
41
42
43
/* First Solution */
function diffArray(arr1, arr2) {
var newArr = [];
// Use map() method to check if the values of arr1 are found in arr2
arr1.map(function(val) {
// if the value from arr1 is not found in arr2, add the value to the newArr array
if (arr2.indexOf(val) === -1) {
newArr.push(val);
}
});
// do the same with arr2
arr2.map(function(val) {
if (arr1.indexOf(val) === -1) {
newArr.push(val);
}
});
return newArr;
}
/* Second Solution */
function diffArray2(arr1, arr2) {
var newArr = arr1.concat(arr2);
return newArr.filter((val) => arr1.indexOf(val) === -1 || arr2.indexOf(val) === -1);
}
/* Third Solution */
function diffArray3(arr1, arr2) {
let arr = arr1.concat(arr2);
let result = [];
for (let i = 0; i < arr.length; i++) {
if (arr1.indexOf(arr[i]) === -1 || arr2.indexOf(arr[i]) === -1) {
result.push(arr[i]);
}
}
return result;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);