Skip to content

Instantly share code, notes, and snippets.

@jupegarnica
Forked from lelouchB/set4.js
Created August 19, 2019 06:16
Show Gist options
  • Select an option

  • Save jupegarnica/d5021db7e509f62f9263b48e2042658c to your computer and use it in GitHub Desktop.

Select an option

Save jupegarnica/d5021db7e509f62f9263b48e2042658c 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