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

Time:06-11

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

CodePudding user response:

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]))

CodePudding user response:

With sorting and counting this should be possible. Since you are incrementing when you find similar characters, this should be okay:

const array1= [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1]

const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1,2]

const array5 = [1,2,3,2,1];
const array6 = [2,2,3,3,4]
function arraysCommon(array1, array2) {
  const ans = [];
  array1.sort();
  array2.sort();
  let j = 0;
  let i = 0;
  while(i<array1.length && j<array2.length){
    if(array1[i] === array2[j]){
      ans.push(array1[i]);
      i  ;
      j  ;
    }
    else if(array2[i] > array1[j]){
    i  ;
    }
    else{
    j  ;
    }
  }
  console.log(ans);
}

arraysCommon(array1,array2);
arraysCommon(array3,array4);
arraysCommon(array5,array6);

CodePudding user response:

this should work as you wanted!

// test 1 
const array1 = [1,4,1,1,5,9,2,7];
const array2 = [1,8,2,5,1];

// test 2
const array3 = [1,2,3,2,1];
const array4 = [5,4,3,2,1];

const mapper = (array1, array2) => {
  var obj = {};
  array1.forEach((x, indexX) => {
    array2.forEach((y, indexY) => {
      if (x == y) {
        if (!Object.values(obj).includes(indexX) && !obj.hasOwnProperty(indexY)) {
          obj[indexY] = indexX;
          return;
        }
      }
    }) 
  })
  return Object.values(obj).sort().map(values => array1[values]);
}

console.log(mapper(array1, array2));
console.log(mapper(array3, array4));

I hope this helps. Cheers.

CodePudding user response:

You can instance a new Set, wich brings only unique values and than retorn a array from this set.

Something like this:

function arraysCommon(array1, array2) {
  const filtered = array1.filter(x => array2.includes(x));
  const uniqueValues = new Set(filtered)
  return Array.from(uniqueValues)
}
  • Related