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

71
Visualizações
Optimise logic to partially match 2 array items

I have two arrays and want to match them partially( i.e. some elements of the first array should be present in the second array but not all). I have a solution but I feel I could use a more efficient one. Here is the use case:

const array_1 = [ 7, 6 ];
const array_2 = [ 4, 6 ,7, 2, 9 ];

const isSomeItemMatched = array_1.some( item => array_2.includes(item) );
const isAllItemMatched = array_1.every( item => array_2.includes(item) );

const isPartiallyMatched = !isAllItemMatched && isSomeItemMatched;

Please let me know if there is an optimized way of doing it.

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

0

Your solution is fine for small arrays. If they get larger, you'll benefit from turning one into a set:

const array_1 = [ 7, 6 ];
const array_2 = [ 4, 6 ,7, 2, 9 ];

function isPartialMatch(a, b) {
    const setA = new Set(a);
    return b.some(x => setA.has(x)) && b.some(x => !setA.has(x));
}

console.log(isPartialMatch(array_1, array_2));

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