////Difference b/w elements of two arrays
const a = [1, 2, 3, 4, 5];
const b = [1, 2];
var difference = a.filter((x) => !b.includes(x));
console.log(difference);
//[3,4,5]
let difference = arr1
.filter(x => !arr2.includes(x))
.concat(arr2.filter(x => !arr1.includes(x)));
function symmetricDiff(arrary1, array2){
let symmetricDifference = [];
symmetricDifference = array1.filter(element => !array2.includes(element))
.concat(array2.filter(element => !array1.includes(element)))
return symmetricDifference;
}
function arr_diff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));
Run code snippet