651 B
651 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Disjointed iterables | snippet | javascript |
|
interior-9 | 2020-10-11T11:53:01+03:00 |
Checks if the two iterables are disjointed (have no common values).
- Use the
Setconstructor to create a newSetobject from each iterable. - Use
Array.prototype.every()andSet.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