Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

70
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda