Javascript Set intersect operation

Description

Javascript Set intersect operation

function intersectSets (setA, setB) {
    var intersection = new Set();
    for (var elem of setB) {
        if (setA.has(elem)) {
            intersection.add(elem);/*from w  ww  .jav a2s. c  o m*/
        }
    }
    return intersection;
}
var setA = new Set([1, 2, 3, 4]),
    setB = new Set([2, 3]);

let a = intersectSets(setA, setB); // Set {2, 3}
console.log(a);



PreviousNext

Related