Created
January 4, 2020 13:22
-
-
Save JosePedroDias/d8c4be002f93dc4c84baa239c8390198 to your computer and use it in GitHub Desktop.
set operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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