I am trying to return an array with only unique elements that do not have duplicates in the array without any special order.
[1,2,3,3,3,4,4,2] will return 1
["hello", "truck", 2, "truck", 2, "truck"] will return "hello"
So far, I have managed to return unique elements using the filter () function, but I'm not sure where to go.
Basically, if there are duplicates, I want both values ββto be removed from the array.
It sounds simple enough, but I have a serious mental hiccup.
Below is my code:
function diff(arr1, arr2) { var newArr = []; newArr = arr1.concat(arr2); newArr = newArr.filter(function(elem, index, self) { return index == self.indexOf(elem); }); console.log(newArr); return newArr; } diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);