π₯ Union of Set
const union = (a, b) => new Set([...a, ...b]);
Usage
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const un = union(a, b);
π₯ Intersection of Set
const intersection = (a, b) => new Set([...a].filter((v) => b.has(v)));
Usage
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const int = intersection(a, b);
π₯ Difference of Set
const difference = (a, b) => new Set([...a].filter((v) => !b.has(v)));
Usage
const a = new Set([1, 2, 3]);
const b = new Set([4, 3, 2]);
const differenceA = difference(a, b);
const differenceB = difference(b, a);
π References