Files
30-seconds-of-code/snippets/isContainedIn.md
30secondsofcode 80a9ec7a2e Travis build: 1682
2020-01-05 19:45:57 +00:00

844 B

title, tags
title tags
isContainedIn array,intermediate

Returns true if the elements of the first array are contained in the second one regardless of order, false otherwise.

Use a for...of loop over a Set created from the first array. Use Array.prototype.some() to check if all distinct values are contained in the second array, use Array.prototype.filter() to compare the amount of occurences of each distinct value in both arrays. Return false if the count of any element is greater in the first array than the second one, true otherwise.


const isContainedIn = (a, b) => {
  for (const v of new Set(a)) {
    if (
      !b.some(e => e === v) ||
      a.filter(e => e === v).length > b.filter(e => e === v).length
    )
      return false;
  }
  return true;
};
isContainedIn([1, 4], [2, 4, 1]); // true