how do you find differences between the first array and the second duplicate?

Put all the numbers of 2nd array in a Set and then use .filter() method on the first array to filter those elements of 1st array that are not present in the Set.

let test1 = ['1', '2', '3', '4/2', '5/4', '6-2'];
let test2 = ['1', '2', '3', '5/4', '4/2', '6-1', '7/2', '8-2'];

const diff = function(arr1, arr2) {
  const set = new Set(arr2);
  return arr1.filter(n => !set.has(n));

console.log(diff(test1, test2));

Note: You could also use !arr2.includes(n) condition in the filter() method but then for each number in arr1, we will go through each element in arr2 which is not ideal. Using a Set, we don’t have to check each element in arr2.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top