Javascript Set intersect

Description

Javascript Set intersect


function intersection(setA, setB) {
    let _intersection = new Set()
    for (let elem of setB) {
        if (setA.has(elem)) {
            _intersection.add(elem)/* w ww.j  a  va2  s. c  om*/
        }
    }
    return _intersection
}


let set1 = new Set();
let set2 = new Set();

set1.add(1);
set1.add(2);
set1.add(3);

set2.add(2);
set2.add(3);


let a = intersection(set1, set2);
console.log(a);



PreviousNext

Related