Javascript Set union

Description

Javascript Set union


function union(setA, setB) {
    let _union = new Set(setA)
    for (let elem of setB) {
        _union.add(elem)/*  w  ww  . ja v  a 2 s .c  o m*/
    }
    return _union
}


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 = union(set1, set2);
console.log(a);



PreviousNext

Related