-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricDifference.js
More file actions
40 lines (27 loc) · 1.27 KB
/
SymmetricDifference.js
File metadata and controls
40 lines (27 loc) · 1.27 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
function sym(arg) {
var args = Array.from(arguments);
var SymmetricDifference = args.reduce(function (arrayA, arrayB) {
var newArray = [];
//take filter values of A out (return false) IF they are in array B
arrayA = arrayA.filter(function (valueInA) {
// console.log(valueInA);
// console.log(arrayB.includes(valueInA));
if (arrayB.includes(valueInA)) {
//delete every instance of valueInA in arrayB //alternatively try using .filter to filter out if valueInA return false
arrayB = arrayB.filter((valueInB) => {
if(valueInB === valueInA) return false;
else return true;
});
// remove from arrayA by returning false to .filter
return false;
} else return true;
});
return arrayA.concat(arrayB);
});
var SymmetricDifferenceCleanedOfDupplicateValues = [];
SymmetricDifference.forEach((element) => {
if (!SymmetricDifferenceCleanedOfDupplicateValues.includes(element)) SymmetricDifferenceCleanedOfDupplicateValues.push(element);
});
return SymmetricDifferenceCleanedOfDupplicateValues;
}
console.log(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]));