Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda