What a set is
A set is a collection of distinct items, called elements or members. Order doesn’t matter in a set, and repeats don’t count as extra members. For example, {1, 2, 2, 3} is the same set as {3, 2, 1}. Sets are written with curly braces, and “x is in A” means that x is an element of A.
Sets can look like arrays at first. People often ask “is an array a data structure?” when they first compare the two, and the main difference is order. An array keeps its order and can hold repeated values, while a set has neither order nor repeats.
This article covers seven types of sets that appear often in introductory courses. Textbooks don’t all agree on how many types to list, so you may see other lists. Subsets and a few related terms appear at the end.
1. Empty set
The empty set has no elements. It is written as ∅ or {}. For example, the set of even prime numbers greater than 2 is empty, because 2 is the only even prime number. The empty set has zero elements, and it is a subset of every set.
2. Singleton set
A singleton set has exactly one element, such as {7}. The set {7} is not the same as the number 7. The set is a container that holds 7.
3. Finite set
A finite set has a fixed, countable number of elements. The set of days in a standard week, {Monday, Tuesday, …, Sunday}, is finite because it has seven elements.
4. Infinite set
An infinite set has no last element. The natural numbers, {1, 2, 3, …}, form an infinite set that can be listed in order, so it is countably infinite. Some infinite sets are uncountable, meaning their elements cannot be matched one-to-one with the natural numbers. The real numbers are an example, a result from Georg Cantor’s work in the late 1800s.
5. Universal set
A universal set contains everything under discussion in a particular problem. Which set counts as universal depends on the context. For example, if U = {1, 2, …, 10} is the universal set and A = {2, 4, 6, 8, 10}, then the complement of A, the elements of U that are not in A, is {1, 3, 5, 7, 9}.
6. Disjoint sets
Two sets are disjoint when they share no elements, so their intersection is the empty set. For example, {1, 2, 3} and {4, 5} are disjoint.
7. Power set
The power set of a set A, written P(A), is the set of all subsets of A. It includes the empty set and A itself. If A has n elements, P(A) has 2^n elements. For A = {a, b}, the power set is {∅, {a}, {b}, {a, b}}, which has 2^2 = 4 elements.
Other terms you may see
- Subset: Every element of set A is also in set B. For example, {1, 2} is a subset of {1, 2, 3}.
- Proper subset: A subset that is not equal to the whole set.
- Equal sets: Two sets with exactly the same elements.
- Equivalent sets: Two sets with the same number of elements, even if the elements differ.
Why sets matter in computing
Sets show up whenever software needs to track which items belong to a group. A hash set stores unique values so that checking membership is fast on average. A graph search keeps a set of visited nodes so it doesn’t revisit the same node endlessly. A database table with a unique key behaves like a set of distinct records.
Understanding set types makes these ideas easier to follow, because each one depends on a clear definition of what it means for an item to belong to a collection.