Files
30-seconds-of-code/snippets/js/s/disjointed-iterables.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

651 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Disjointed iterables snippet javascript
array
interior-9 2020-10-11T11:53:01+03:00

Checks if the two iterables are disjointed (have no common values).

  • Use the Set constructor to create a new Set object from each iterable.
  • Use Array.prototype.every() and Set.prototype.has() to check that the two iterables have no common values.
const isDisjoint = (a, b) => {
  const sA = new Set(a), sB = new Set(b);
  return [...sA].every(v => !sB.has(v));
};
isDisjoint(new Set([1, 2]), new Set([3, 4])); // true
isDisjoint(new Set([1, 2]), new Set([1, 3])); // false