This function finds the difference of two sets, i.e., elements that are in the first set but not in the second set.
JavaScript:
let difference = (set1, set2) => new Set([...set1].filter(val => !set2.has(val)));
// Example
console.log(difference(new Set([1, 2, 3]), new Set([2, 3, 4]))); // Set [1]
console.log(difference(new Set(['a', 'b', 'c']), new Set(['b', 'c', 'd']))); // Set ['a']
TypeScript:
let difference = (set1: Set, set2: Set): Set => new Set([...set1].filter(val => !set2.has(val)));
// Example
console.log(difference(new Set([1, 2, 3]), new Set([2, 3, 4]))); // Set [1]
console.log(difference(new Set(['a', 'b', 'c']), new Set(['b', 'c', 'd']))); // Set ['a']