Set Operations
Set Operations
Section titled “Set Operations”Introduction
Section titled “Introduction”JavaScript’s Set doesn’t have built-in set operation methods, but you can easily implement union, intersection, and difference using array methods.
The union combines elements from both sets, removing duplicates:
const setA = new Set([1, 2, 3, 4]);const setB = new Set([3, 4, 5, 6]);
const union = new Set([...setA, ...setB]);console.log([...union]); // [1, 2, 3, 4, 5, 6]Intersection
Section titled “Intersection”The intersection returns elements that exist in both sets:
const setA = new Set([1, 2, 3, 4]);const setB = new Set([3, 4, 5, 6]);
const intersection = new Set( [...setA].filter(x => setB.has(x)));console.log([...intersection]); // [3, 4]Difference
Section titled “Difference”The difference returns elements in the first set that are not in the second:
const setA = new Set([1, 2, 3, 4]);const setB = new Set([3, 4, 5, 6]);
const difference = new Set( [...setA].filter(x => !setB.has(x)));console.log([...difference]); // [1, 2]
// Symmetric difference (elements in either but not both)const symmetricDiff = new Set( [...setA].filter(x => !setB.has(x)).concat( [...setB].filter(x => !setA.has(x)) ));console.log([...symmetricDiff]); // [1, 2, 5, 6]Subset Check
Section titled “Subset Check”function isSubset(subset, superset) { return [...subset].every(x => superset.has(x));}
const setA = new Set([1, 2]);const setB = new Set([1, 2, 3, 4]);
console.log(isSubset(setA, setB)); // trueconsole.log(isSubset(setB, setA)); // falsePractical Example
Section titled “Practical Example”const allUsers = new Set(['Alice', 'Bob', 'Charlie', 'Diana']);const activeUsers = new Set(['Alice', 'Charlie', 'Eve']);const adminUsers = new Set(['Alice', 'Diana']);
// Active adminsconst activeAdmins = new Set( [...activeUsers].filter(u => adminUsers.has(u)));console.log([...activeAdmins]); // ['Alice']
// Users who are NOT activeconst inactiveUsers = new Set( [...allUsers].filter(u => !activeUsers.has(u)));console.log([...inactiveUsers]); // ['Bob', 'Diana']Summary
Section titled “Summary”| Operation | Code | Result |
|---|---|---|
| Union | [...setA, ...setB] | All unique from both |
| Intersection | [...setA].filter(x => setB.has(x)) | Common elements |
| Difference | [...setA].filter(x => !setB.has(x)) | In A but not B |
| Symmetric Diff | Both differences combined | In A or B but not both |