Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!