Search code examples
javascriptarraysduplicatesintersection

A function that returns an array which is the intersection of two other arrays


function arraysCommon(array1, array2) {
  return array1.filter(x => array2.includes(x));
}

This function does not work the way I want it to. For instance given array1 = [1,2,3,2,1] and array2 = [5,4,3,2,1] it returns [1,2,3,2,1], since the elements 1,2,3 are seen in both arrays. But I want it to return [1,2,3] in that order since 1,2,3 are seen only once in array2 and are treated as seperate entities.

So pretty much the functionality should be that

  • Each element in the first array can map to at most one element in the second array.
  • Duplicated elements in each array are treated as separate entities.
  • the first array determines the order

I have attempted to loop through the arrays and check and compare the number of duplicates in each array but I can't seem to get the logic working correctly. Is there a different way to approach this?

I've attached an image of two Venn diagrams that might clarify the difference I've attached an image of two Venn diagrams that might clarify the difference


Solution

  • Unfortunately, it gets more complicated because you need to know what numbers you have already added. In this case you need a temporary array to hold the result. We also need to track if a number exists in the array two times.

    Try this:

    function arraysCommon(array1, array2) {
      //Copy array2 by duplicating and spreading the elements into another array.
      var copyArray2 = [...array2];
      //Temperary Array
      var temp = [];
      for (let x of array1) {
          //Check if the element is in the first array and not already added to the temp array
          if (copyArray2.includes(x)) {
              temp.push(x);
              //Remove item from copy array2 so it cannot be used anymore
              copyArray2.splice(copyArray2.indexOf(x), 1);
          }
      } 
      //Return the temp array
      return temp;
    }
    
    console.log(arraysCommon([1,2,3,2,1], [5,4,3,2,1]))
    console.log(arraysCommon([1,2,3,2,1], [2,2,3,3,4]))