Why This Matters
A set is one of the most fundamental concepts in mathematics and computer science. It is an unordered collection of distinct elements. Every database table is essentially a set of rows. Every type in a type system defines a set of valid values. Every access control list is a set of permissions. Understanding sets gives you a precise vocabulary for reasoning about collections, membership, and relationships.
Union combines two sets into one containing all elements from both. Intersection finds the elements that two sets share. Difference removes one set from another. These three operations appear everywhere: SQL UNION, INTERSECT, and EXCEPT; permission merging in access control; combining search results from multiple sources.
Set theory is also the language of predicate logic. Set-builder notation uses predicates to define membership. Quantifiers range over sets. Understanding sets deepens your grasp of logic, types, and data modeling.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Sets are unordered collections of unique elements. Union, intersection, and difference are the three core operations.
Code Example
// Sets in JavaScript use the Set object
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
// Membership
console.log("2 in A?", a.has(2)); // true
console.log("5 in A?", a.has(5)); // false
// Union: all elements from both sets
function setUnion(setA, setB) {
return new Set([...setA, ...setB]);
}
console.log("Union:", [...setUnion(a, b)]); // [1,2,3,4,5,6]
// Intersection: elements in both sets
function setIntersection(setA, setB) {
return new Set([...setA].filter(x => setB.has(x)));
}
console.log("Intersection:", [...setIntersection(a, b)]); // [3,4]
// Difference: elements in A but not in B
function setDifference(setA, setB) {
return new Set([...setA].filter(x => !setB.has(x)));
}
console.log("A - B:", [...setDifference(a, b)]); // [1,2]
console.log("B - A:", [...setDifference(b, a)]); // [5,6]
// Subset check
function isSubset(setA, setB) {
return [...setA].every(x => setB.has(x));
}
console.log("Is {3,4} subset of A?", isSubset(new Set([3,4]), a)); // true
console.log("Is A subset of B?", isSubset(a, b)); // false
// Sets automatically remove duplicates
const dupes = new Set([1, 1, 2, 2, 3]);
console.log("Deduped:", [...dupes]); // [1, 2, 3]Interactive Experiment
Try these exercises:
- Create two sets with some overlap and compute their union, intersection, and difference. Verify the results manually.
- Show that union is commutative: A union B equals B union A. Is difference commutative? Test it.
- Verify De Morgan's Law for sets: the complement of (A union B) equals (complement A) intersect (complement B). Use a universal set U = 1 through 10.
- What is the intersection of a set with the empty set? What is the union with the empty set?
- Create a set from an array with duplicates and observe the automatic deduplication. How many elements remain?
Quick Quiz
Coding Challenge
Write three functions: `setUnion(a, b)` returns an array of all unique elements from both arrays, `setIntersection(a, b)` returns an array of elements in both arrays, and `setDifference(a, b)` returns an array of elements in a but not in b. All results should be sorted in ascending numeric order.
Real-World Usage
Set operations power many software systems:
- SQL queries: UNION, INTERSECT, and EXCEPT perform set operations on query results. JOIN conditions use set intersection logic.
- Access control: Permission systems merge role-based sets. A user with roles [admin, editor] gets the union of both permission sets.
- Search engines: Combining results from multiple indexes uses union. Filtering by multiple criteria uses intersection.
- Version control: Git diff computes the set difference between two commits to show changed files.
- Deduplication: Converting a list to a set instantly removes duplicates, a common data cleaning operation.