Superset of iterable

JavaScript, Array · Oct 22, 2020

Checks if the first iterable is a superset of the second one, excluding duplicate values.

const superSet = (a, b) => {
  const sA = new Set(a), sB = new Set(b);
  return [...sB].every(v => sA.has(v));
};
superSet(new Set([1, 2, 3, 4]), new Set([1, 2])); // true
superSet(new Set([1, 2, 3, 4]), new Set([1, 5])); // false

More like this

  • Subset of iterable

    Checks if the first iterable is a subset of the second one, excluding duplicate values.

    JavaScript, Array · Oct 22, 2020

  • Array is contained in other array

    Checks if the elements of the first array are contained in the second one regardless of order.

    JavaScript, Array · Oct 22, 2020

  • Tip: Make any JavaScript value iterable

    Did you know you can define an iterator for any JavaScript value? This quick tip will show you how.

    JavaScript, Array · Jun 12, 2021