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

187
Views
Determine if one array contains all elements of another array, including any duplicates

I'm looking for a function that returns true if and only if a given array includes all the elements of another target array which may include two or more occurrences of the same element.

Sample data:

const target = [ 1, 3, 3 ];
const array1 = [ 1, 2, 3, 4 ]; // false
const array2 = [ 1, 3, 3, 4 ]; // true
const array3 = [ 1, 3, 4, 3 ]; // true
const array4 = [ 3, 2, 1, 3 ]; // true
const array5 = [ 1, 1, 3, 3 ]; // true

This question's answer is close but doesn't account for duplicates in target array.

This question's answer works for Ruby but I'm looking for a Javascript solution.

When not accounting for duplicates, .indexOf() and .includes() make it rather easy and there are elegant one-liner solutions. But I'm unsure how to keep track of duplicates and suspect a more iterative approach is needed.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const isMultiSubset = (target, value) => {
  const occurences = new Map;
  for(const entry of target) 
    occurences.set(entry, (occurences.get(entry) ?? 0) + 1);

  for(const entry of value)
    if (occurences.has(entry))
       occurences.set(entry, occurences.get(entry) - 1);

  return [...occurences.values()].every(count => count <= 0);   
};

By using a Map to count occurences this can be solved in O(n + m).

about 4 years ago · Juan Pablo Isaza Report

0

Similar to @Barmar's comment, you might also loop over the elements you want to have included and remove them from the target, if they are present and immediately return false if one of them is not present. If every desired element was found, return true.

Notice: this has time complexity O(n*m), @Jonas' solution has better time complexity.

function includesMulti(elements, inArray) {
  const unmatched = inArray.slice();
  for (const element of elements) {
    const matchIndex = unmatched.indexOf(element);
    if (matchIndex === -1) return false;
    unmatched.splice(matchIndex, 1);
  }
  return true;
}

const target = [ 1, 3, 3 ];
const array1 = [ 1, 2, 3, 4 ]; // false
const array2 = [ 1, 3, 3, 4 ]; // true
const array3 = [ 1, 3, 4, 3 ]; // true
const array4 = [ 3, 2, 1, 3 ]; // true
const array5 = [ 1, 1, 3, 3 ]; // true
const array6 = [ 1, 1, 3, 3];  // true

console.log(includesMulti(target, array1));
console.log(includesMulti(target, array2));
console.log(includesMulti(target, array3));
console.log(includesMulti(target, array4));
console.log(includesMulti(target, array5));
console.log(includesMulti(target, array6));

A refinement of this approach which avoids cloning and rescanning the array to search in could be the following: for each element to look for remember where it was found last use that as the offset for the next indexOf search.

function includesMulti(elements, inArray) {
  const lastMatches = new Map();
  for (const element of elements) {
    const previousMatchIndex = lastMatches.get(element);
    const matchIndex = inArray.indexOf(element, previousMatchIndex + 1);
    if (matchIndex === -1) return false;
    lastMatches.set(element, matchIndex);
  }
  return true;
}

const target = [ 1, 3, 3 ];
const array1 = [ 1, 2, 3, 4 ]; // false
const array2 = [ 1, 3, 3, 4 ]; // true
const array3 = [ 1, 3, 4, 3 ]; // true
const array4 = [ 3, 2, 1, 3 ]; // true
const array5 = [ 1, 1, 3, 3 ]; // true
const array6 = [ 1, 1, 3, 3];  // true

console.log(includesMulti(target, array1));
console.log(includesMulti(target, array2));
console.log(includesMulti(target, array3));
console.log(includesMulti(target, array4));
console.log(includesMulti(target, array5));
console.log(includesMulti(target, array6));

There is a little trick in here: when no previous match was found, undefined is returned from map.get and then previousMatchIndex+1 is NaN which is ignored by indexOf. To be more explicit, you might want to replace that by previousMatchIndex === undefined ? 0 : (previousMatchIndex+1).

about 4 years ago · Juan Pablo Isaza Report

0

Something like this maybe. It works but it is not the most performant.

const target = [1, 3, 3];
const array1 = [1, 2, 3, 4]; // false
const array2 = [1, 3, 3, 4]; // true
const array3 = [1, 3, 4, 3]; // true
const array4 = [3, 2, 1, 3]; // true

const testArray = (arr1, arr2) => {
    const countOccurrences = (arrayToCount) => {
        return arrayToCount.reduce((acc, e) => {
            acc[e] ? (acc[e] = { count: acc[e].count + 1 }) : (acc[e] = { count: 1 });
            return acc;
        }, {});
    };
    let reduced = countOccurrences(arr1);
    let reduced2 = countOccurrences(arr2);

    const result = Object.keys(reduced).reduce((acc, e) => {
        if (!acc) return false;
        if (!reduced2[e]) return false;
        if (reduced2[e].count < reduced[e].count) return false;
        return acc;
    }, true);

    return result;
};

console.log(testArray(target, array1));
console.log(testArray(target, array2));
console.log(testArray(target, array3));
console.log(testArray(target, array4));
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!