This function finds the union of two sets, i.e., elements that are in either of the two sets.
JavaScript:
let union = (set1, set2) => new Set([...set1, ...set2]);
// Example
console.log(union(new Set([1, 2, 3]), new Set([2, 3, 4]))); // Set [1, 2, 3, 4]
console.log(union(new Set(['a', 'b', 'c']), new Set(['b', 'c', 'd']))); // Set ['a', 'b', 'c', 'd']
TypeScript:
let union = (set1: Set, set2: Set): Set => new Set([...set1, ...set2]);
// Example
console.log(union(new Set([1, 2, 3]), new Set([2, 3, 4]))); // Set [1, 2, 3, 4]
console.log(union(new Set(['a', 'b', 'c']), new Set(['b', 'c', 'd']))); // Set ['a', 'b', 'c', 'd']