Sets
A set is a collection of distinct things — and once you can ask what is in it, you can combine sets by union and intersection.
A fruit bowl holds an apple, a pear, and a banana. It does not matter what order they sit in, and a second apple would not make it “more of a bowl” — it is just a collection of distinct things. Mathematics calls such a collection a set. By the end of this lesson you will be able to combine any two such collections and say exactly what they share — a skill that underlies databases, filters, and every permission system you will ever work with.
After this lesson you can say what a set is, decide whether something is in a set, give the size of a set, and combine two sets by union and intersection.
A set is a collection of distinct things. The things in a set are its
elements. We write a set by listing its elements inside curly brackets: the set
{1, 2, 3} has the elements 1, 2, and 3. Order does not matter, and an element is
either in or out — listing it twice changes nothing.
The basic question about a set is membership: is this thing in it? For the set
{1, 2, 3}, the number 2 is an element — it is in the set. The number 5 is not an
element — it is out. Every set splits the world into two: things that belong, and
things that do not.
The size of a set is how many elements it has. The set {1, 2, 3} has size 3. A
set can also be empty — the empty set has no elements at all, and its size is 0.
The empty set is still a perfectly good set; it is just the collection of nothing.
Two sets combine by union and intersection. Think about it: when you query a database for users who are admins or subscribers, you want their union; when you want admins who are also subscribers, you want their intersection. The union of two sets is everything that is in either one — all the elements gathered together, each listed once. The intersection is everything that is in both — only the elements the two sets share. Union is the generous combination; intersection is the strict one.
Set A is {1, 2, 3} and set B is {2, 3, 4}. Find their union and their
intersection.
The union gathers every element that is in A or in B, each counted once. From A:
1, 2, 3. From B the new ones: 4. The union is {1, 2, 3, 4} — size 4.
The intersection keeps only elements in both A and B. Is 1 in both? No, only A.
Is 2 in both? Yes. Is 3 in both? Yes. Is 4 in both? No, only B. The intersection is
{2, 3} — size 2.
Union is always at least as big as either set; intersection is always at most as big as the smaller set.
▸Why this works
Why does listing an element twice change nothing? Because a set only records whether
something belongs, not how many times. Membership is a yes-or-no question — like the
truth values from earlier in this unit. {1, 1, 2} and {1, 2} are the same set:
both answer “is 1 in?” with yes and “is 2 in?” with yes. There is no “how many copies”
to track.
▸Common mistake
A common mistake is mixing up union and intersection — taking the intersection of
{1,2,3} and {2,3,4} to be the larger {1,2,3,4}. Intersection keeps only the
shared elements, so it is the smaller {2,3}. Union gathers everything; intersection
keeps only the overlap. When in doubt: union means “or”, intersection means “and”.
How many elements are in the set {2, 4, 6, 8}? Type the size.
What is the size of the empty set? Type the number.
A is {1, 2, 3} and B is {3, 4}. What is the size of their union? Type the number.
A is {1, 2, 3} and B is {3, 4}. What is the size of their intersection? Type the number.
Is the number 5 an element of the set {1, 2, 3}? Type 1 for yes, 0 for no.
What is the intersection of two sets?
A set is a collection of distinct things called its elements, written inside curly brackets; order and repeats do not matter. The basic question about a set is membership — whether a thing is in it. The size of a set is its element count, and the empty set has size 0. Two sets combine by union (everything in either) and intersection (only what is in both). Now when you see a filter, a JOIN condition, or a permission check, you will ask yourself: am I looking for a union or an intersection — and you will immediately know which one to reach for.
Practice
Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.
Something unclear?
Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.
Apply this
Put this lesson to work on a real build.