Home > Enterprise >  Two Arrays: Find items that are missing in the first, find items that are missing in the second
Two Arrays: Find items that are missing in the first, find items that are missing in the second

Time:03-15

Imagine two arrays:

const array1 = [1,2,3];
const array2 = [2,3,4];

Now, I want to get all the differences of these two arrays and put them in two new arrays. One Array will be for all the items that where missing in the first. The other will be for the items missing in the second. The result would look something like this:

const newArray1 = [1];
const newArray2 = [4];

How would I go about this and what is the most efficient way?

CodePudding user response:

Something like this would be relatively efficient, you're only iterating through two arrays.

Iterate through the first array and add any missing items to the first newArray. Repeat the same process for the second array.

If you need to only loop through the array once, you would need to do something similar to what is done below, but only loop through the longest array.

const array1 = [1,2,3];
const array2 = [2,3];

function diff (arr1, arr2) {
  const newA1 = [];
  const newA2 = [];
  arr1.forEach((item, i) => {
    let index = arr2.findIndex(it => it === item);
    if(index < 0) newA1.push(item);
  });
  arr2.forEach((item, i) => {
    let index = arr1.findIndex(it => it === item);
    if(index < 0) newA2.push(item);
  });
  return [newA1, newA2];
}

console.log(diff(array1, array2));

More efficent method (only loop through 1 array). This way you choose the longest array, loop through it, and check for duplicates of the current item of the long array and if an item in the secondary array exists at the same position, check for duplicates of this item in the longest array as well. This method is similar to the method above, but there is only 1 for each loop.

const array1 = [1,2,3];
const array2 = [3,4,5];

function diff (arr1, arr2) {
  const newA1 = [];
  const newA2 = [];
  let baseArr, secondaryArr;
  
  if(arr1.length > arr2.length) {
    baseArr = arr1;
    secondaryArr = arr2;
  } else {
    baseArr = arr2;
    secondaryArr = arr1;
  }
  
  baseArr.forEach((item, i) => {
    const secondaryArrI = secondaryArr.findIndex(it => it === item);
    if(secondaryArrI < 0) newA1.push(item)
    if(typeof secondaryArr[i] !== "undefined") {
      const removeI = baseArr.findIndex(it => it === secondaryArr[i]);
      
      if(removeI < 0) newA2.push(secondaryArr[i]);
    
    }
  })
  
  return [newA1, newA2];
}

console.log(diff(array1, array2));

CodePudding user response:

const array1 = [2,1,3,5];
const array2 = [4,3,2,6,7];

function diff(arr1, arr2) {
    arr1.sort();
    arr2.sort();
    let a1 = [];
    let a2 = [];
    let i = 0;
    let j = 0;
    while (i < array1.length || j < array2.length) {
        if (i >= arr1.length) {
         a2.push(arr2[j]);
         j  ;
      } else if (j >= array2.length) {
         a1.push(arr1[i]);
         i  ;
      }  else if (arr1[i] < arr2[j]) {
         a1.push(arr1[i]);
         i  ;
      } else if (arr2[j] < arr1[i]) {
         a2.push(arr2[j]);
         j  ;
      } else {
         // Same value, do nothing
         i  ;
         j  ;
      }
    }
    return [a1, a2];
}

console.log(diff(array1, array2));
// OUTPUT: [[1, 5], [4, 6, 7]]

Here's another potential implementation using sorting, but it has the side effect of leaving array1 and array2 in a sorted fashion. If this is a problem, then use a deep copy of array1 and array2 before calling sort.

  • Related