Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Note
You can return the array with its elements in any order.
I am trying to solve this by looping through each element of the array and saying if there is more than one of these elements in the array then we don't want it to be returned.
NB: I have taken it one step further than the question, and am essentially trying to solve: "Return any elements that only occur once across either of the arrays." How can I reject any element that occurs more than once?
My code, which doesn't work:
function diffArray(arr1, arr2) {
var newArr = [...arr1, ...arr2];
let a = newArr.forEach();
function getOccurrence(newArr, a) {
return newArr.filter((v) => (v === value)).length;
}
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);