Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created January 4, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/d8c4be002f93dc4c84baa239c8390198 to your computer and use it in GitHub Desktop.
Save JosePedroDias/d8c4be002f93dc4c84baa239c8390198 to your computer and use it in GitHub Desktop.
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