Skip to content

Instantly share code, notes, and snippets.

@lelouchB
Created August 11, 2019 05:32
Show Gist options
  • Select an option

  • Save lelouchB/2ef99d247967eee3315c0dc594d43553 to your computer and use it in GitHub Desktop.

Select an option

Save lelouchB/2ef99d247967eee3315c0dc594d43553 to your computer and use it in GitHub Desktop.
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