Skip to content

Set Operations

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]

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]

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]
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)); // true
console.log(isSubset(setB, setA)); // false
const allUsers = new Set(['Alice', 'Bob', 'Charlie', 'Diana']);
const activeUsers = new Set(['Alice', 'Charlie', 'Eve']);
const adminUsers = new Set(['Alice', 'Diana']);
// Active admins
const activeAdmins = new Set(
[...activeUsers].filter(u => adminUsers.has(u))
);
console.log([...activeAdmins]); // ['Alice']
// Users who are NOT active
const inactiveUsers = new Set(
[...allUsers].filter(u => !activeUsers.has(u))
);
console.log([...inactiveUsers]); // ['Bob', 'Diana']
OperationCodeResult
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 DiffBoth differences combinedIn A or B but not both