Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
set operations
// s1 contains all elements of s2
function setContains(s1, s2) {
return new Set([...s2].every(x => s1.has(x)));
}
// elements in both s1 and s2
function setIntersection(s1, s2) {
return new Set([...s1].filter(x => s2.has(x)));
}
// elements only existing in s1
function setDifference(s1, s2) {
return new Set([...s1].filter(x => !s2.has(x)));
}
function setEqual(s1, s2) {
return s1.length === s2.length && setContains(s1, s2);
}
module.exports = {
setContains,
setIntersection,
setDifference,
setEqual,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.