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

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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