-
-
Save jupegarnica/d5021db7e509f62f9263b48e2042658c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function union(setA, setB) { | |
| var _union = new Set(setA); | |
| for (var elem of setB) { | |
| _union.add(elem); | |
| } | |
| return _union; | |
| } | |
| function intersection(setA, setB) { | |
| var _intersection = new Set(); | |
| for (var elem of setB) { | |
| if (setA.has(elem)) { | |
| _intersection.add(elem); | |
| } | |
| } | |
| return _intersection; | |
| } | |
| function difference(setA, setB) { | |
| var _difference = new Set(setA); | |
| for (var elem of setB) { | |
| _difference.delete(elem); | |
| } | |
| return _difference; | |
| } | |
| //Examples | |
| var setA = new Set([1, 2, 3, 4]); | |
| var setB = new Set([2, 3]); | |
| var setC = new Set([3, 4, 5, 6]); | |
| union(setA, setC); // => Set [1, 2, 3, 4, 5, 6] | |
| intersection(setA, setC); // => Set [3, 4] | |
| difference(setA, setC); // => Set [1, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment