Set Theory
Set Theory might sound academic, but it’s everywhere in development - from deduplicating data to writing cleaner algorithms or SQL queries.
What is a Set?
A set is a well-defined, unordered collection of distinct elements. Think of it as a way to group things without duplicates.
Notation
-
Tabular form:
A = {apple, banana, orange}
-
Set-builder form:
B = {x | x is a prime number < 10} → {2, 3, 5, 7}
Types of Set
Finite Set
const permissions = new Set(['read', 'write', 'delete'])
Infinite Set
function naturalNumbers() {
let n = 1;
while (true) n++;
}
Empty Set
const emptySet = new Set()
Common Set Operations
1. Union (∪) - Combine all unique elements
A = {1, 2, 3}
B = {3, 4}
A U B = {1, 2, 3, 4, 5}
2. Intersection (∩) - Common elements
A = {1, 2, 3}
B = {3, 4}
A ∩ B = {3}
3. Difference (−) - Elements in A but not in B
A = {1, 2, 3}
B = {3, 4}
A-B = {1, 2}
How Developers Use Sets
De-duplication
Sets help remove duplicates from lists like tags, cache, or user inputs.
const cache = new Set();
cache.add('user123');
cache.add('user123'); // Duplicate ignored
Databases
Set theory powers common SQL operations like UNION
, INTERSECT
, and EXCEPT
.
SELECT id FROM active_users
UNION
SELECT id FROM premium_users;
Role-Based Access
const adminPermissions = new Set(['read', 'write', 'delete']);
function hasPermission(roleSet, action) {
return roleSet.has(action);
}
hasPermission(adminPermissions, 'delete'); // true
Conclusion
Set Theory helps developers write cleaner, more efficient code. Whether it’s handling unique values, writing SQL queries sets are a fundamental tool worth mastering.