Javascript Set difference

Description

Javascript Set difference


function difference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        _difference.delete(elem)//from  ww  w .  ja va 2s. c om
    }
    return _difference
}

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



PreviousNext

Related