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

215
Vistas
Check to see if an array contains all elements of another array, including whether duplicates appear twice

I need to check whether one array contains all of the elements of another array, including the same duplicates. The second array can have extra elements. I'm using every...includes, but it's not catching that the second array doesn't have the right duplicates.

For example:

const arr1 = [1, 2, 2, 3, 5, 5, 6, 6]
const arr2 = [1, 2, 3, 5, 6, 7]

if(arr1.every(elem => arr2.includes(elem))){
   return true     // should return false because arr2 does not have the same duplicates

}

Thanks!

Edit: arr1 is one of many arrays that I am looping through which are coming out of a graph traversal algorithm, so I'd like to avoid restructuring them into an object to create a dictionary data structure if possible.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

        const arr1 = [1, 2, 2, 3, 5, 5, 6, 6];
        //const arr2 = [1, 2, 3, 5, 6, 7];
        const arr2 = [1, 2, 2, 3, 5, 5];
        let includesAll1 = true;
        let includesAll2 = true;
        const checkObj1 = {

        };

        const checkObj2 = {

        };

        arr1.forEach((el)=> {
            if(checkObj1[el] === undefined) {
                checkObj1[el] = 1;
            } else {
                checkObj1[el]++;
            }
        });

        arr2.forEach((el)=> {
            if(checkObj2[el] === undefined) {
                checkObj2[el] = 1;
            } else {
                checkObj2[el]++;
            }
        });

        const check1Keys = Object.keys(checkObj1);
        const check2Keys = Object.keys(checkObj2);

        if(check1Keys.length > check2Keys.length) {
            includesAll2 = false;

            check2Keys.forEach((key)=> {
                const value1 = checkObj1[key];
                const value2 = checkObj2[key];

                if(!arr1.includes(parseInt(key)) || value1 != value2) {
                    includesAll1 = false;
                }
            });
        } else {
            includesAll1 = false;

            check1Keys.forEach((key)=> {
                const value1 = checkObj1[key];
                const value2 = checkObj2[key];
                console.log(value1, value2, key);

                if(!arr2.includes(parseInt(key)) || value1 != value2) {
                    includesAll2 = false;
                }
            });
        }

        console.log(includesAll1);
        console.log(includesAll2);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Does this solve your problem?

const arr = [1, 2, 3, 5, 6, 7, 2, 10, 2, 3, 2];
const subArr = [1, 2, 2, 3, 2] 
const contains = subArr.every(num => subArr.filter(n => n == num).length <= arr.filter(n => n== num).length);
console.log(contains);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try creating this function:


 function containsAll (target, toTest) {

    const dictionary = {}

    target.forEach(element => {
        if (dictionary[element] === undefined) {
            dictionary[element] = 1;
            return;
        }
        dictionary[element]++;
    });


    toTest.forEach(element => {
        if (dictionary[element] !== undefined)
            dictionary[element]--;
    })

    for (let key in dictionary) {
        if (dictionary[key] > 0) return false;
    }

    return true;

}

Then invoke it like this:

const arr1 = [1, 2, 2, 3, 5, 5, 6, 6]
const arr2 = [1, 2, 3, 5, 6, 7]


console.log(containsAll(arr1, arr2)) // returns false
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