0

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]);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
AndrewNeedsHelp
  • 385
  • 4
  • 15
  • 2
    https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – Dima Vak May 12 '20 at 13:25
  • check this https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – FC1927DK May 12 '20 at 13:27
  • Does this answer your question? [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – Joseph Cho May 12 '20 at 13:28

5 Answers5

0

Simple way would be something like this

let array1 = [1,2,3,4,5,6,7]
let array2 = [51,2,3,5,7]

let in1ButNot2 = array1.filter(x=>array2.indexOf(x) === -1)
let difference = array2.filter(y=>in1ButNot2.indexOf(y) === -1)

console.log(difference)

The point is that youre using .indexOf to see if an element is present in the array, and filtering based on that

pacukluka
  • 728
  • 4
  • 18
0

If you wants to find unique element you can use this

const arr1 = [1, 2, 3, 5];
const arr2 = [1, 2, 3, 4, 5];

let unique1 = arr1.filter((o) => arr2.indexOf(o) === -1);
let unique2 = arr2.filter((o) => arr1.indexOf(o) === -1);

const unique = unique1.concat(unique2);

console.log(unique);
// >> [4]

I think it will help.

0

You can have a look at this fiddle

which I will explain here:

const arrayDifference = (arr1, arr2) => {
  return arr1.filter(x => !arr2.includes(x))
    .concat(arr2.filter(x => !arr1.includes(x)));
};

this part returns items in arr1 which are not included in arr2

arr1.filter(x => !arr2.includes(x))

while this other part

arr2.filter(x => !arr1.includes(x))

returns items in arr2 which are not in arr1

then the .concat merge them, returning your symmetric difference

CapitanFindus
  • 1,498
  • 15
  • 26
0
const diffArray = (arr1, arr2) => {
  return arr1.filter((item, index) => arr2.indexOf(item) > 0)
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]))
0

Given Data:

const arr1 = [10, 20, 30, 40]
const arr2 = [10, 20, 50, 60]

Expected Result:

Result: [30, 40, 50, 60];

The solution is:


function diffArray(arr1, arr2){
    // combine elements from both arrays
    const allElements = [...arr1, ...arr2]

    // filter elements not available in arr1
    const filteredArr1 = allElements.filter(element => !arr1.includes(element));

    // filter elements not available in arr2
    const filteredArr2 = allElements.filter(element => !arr2.includes(element));

    // return filtered elements from both arr1 and arr2
    return [...filteredArr1, ...filteredArr2];
}


// testing the code
const result = diffArray([10, 20, 30, 40], [10, 20, 50, 60])
console.log(result);

Note: The result would be unsorted, you can use sort() function to sort it.

I hope the above solution have provided you some idea and explanation.

Jayant Malik
  • 1,328
  • 8
  • 11