Wednesday, June 27, 2018

Union and Intersection in Javascript Array in ES6

By using Set in ES6 easily we can achieve

public union(setA, setB) {
let _union = new Set(setA);
for (var elem of setB) {
_union.add(elem);
}
return _union;
}
public intersection(setA, setB) {
let _intersection = new Set();
for (let elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}

No comments:

Post a Comment