Javascript Set symmetric difference

Description

Javascript Set symmetric difference


function symmetricDifference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        if (_difference.has(elem)) {
            _difference.delete(elem)/*from www  .j  a v a 2 s . c o  m*/
        } else {
            _difference.add(elem)
        }
    }
    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 = symmetricDifference(set1, set2);
console.log(a);



PreviousNext

Related