A visual guide to counting unions without double-counting
The inclusion-exclusion principle is a fundamental counting technique that helps us determine the size of a union of sets. The challenge? When we naively add up the sizes of sets, we double-count elements that appear in multiple sets.
Venn diagrams make this principle crystal clear. Let’s build up our understanding starting with two sets.
The Problem: Naïve Counting Fails¶
Suppose we want to count how many elements are in “A or B” (written as , the union).
Our first instinct might be:
Why is this wrong? Because elements in both A and B (the intersection ) get counted twice!
The inclusion-exclusion principle fixes this:
Let’s visualize why.
Two Sets: The Foundation¶
Matplotlib is building the font cache; this may take a moment.

The Two-Set Formula¶
Why it works:
Start by adding and — this counts everything in both sets
But wait! Elements in were counted twice (once in , once in )
Subtract once to correct the double-counting
Note: (intersection is commutative) — order doesn’t matter!
Example 1: Student Clubs¶
Scenario: At a school:
45 students are in the Drama Club (set D)
35 students are in the Chess Club (set C)
15 students are in both clubs (D ∩ C)
Question: How many students are in at least one club?
Solution¶
Using inclusion-exclusion:
Answer: 65 students are in at least one club.

Extending to Three Sets¶
With three sets, the counting becomes trickier. We need to:
Include all three sets individually
Exclude all pairwise intersections (we double-counted them)
Include the triple intersection back (we excluded it too many times!)
The formula:
Let’s visualize why each term is necessary.

Why Each Term in the Three-Set Formula?¶
Understanding the counting:
| Region | Times in sum | After subtracting pairs | After adding triple |
|---|---|---|---|
| A only | 1x | 1x | 1x (correct) |
| B only | 1x | 1x | 1x (correct) |
| C only | 1x | 1x | 1x (correct) |
| A∩B only | 2x | 2x - 1x = 1x | 1x (correct) |
| A∩C only | 2x | 2x - 1x = 1x | 1x (correct) |
| B∩C only | 2x | 2x - 1x = 1x | 1x (correct) |
| A∩B∩C | 3x | 3x - 3x = 0x | 0x + 1x = 1x (correct) |
The center region (A∩B∩C) is:
Added 3 times (once each in |A|, |B|, |C|)
Subtracted 3 times (once each in |A∩B|, |A∩C|, |B∩C|)
Net = 0, so we add it back once with +|A∩B∩C|
Example 2: Programming Languages Survey¶
Scenario: A survey of 100 programmers asks about language proficiency:
Python (P): 50 programmers
JavaScript (J): 40 programmers
Rust (R): 30 programmers
Overlaps:
P ∩ J: 20 programmers know both Python and JavaScript
P ∩ R: 15 programmers know both Python and Rust
J ∩ R: 10 programmers know both JavaScript and Rust
P ∩ J ∩ R: 8 programmers know all three
Question: How many programmers know at least one of these languages?
Solution¶
Answer: 83 programmers know at least one of Python, JavaScript, or Rust.

Example 3: Probability Version¶
The inclusion-exclusion principle also works for probabilities!
Scenario: Rolling a fair six-sided die:
Event A: Roll is divisible by 2 (outcomes: {2, 4, 6})
Event B: Roll is divisible by 3 (outcomes: {3, 6})
Event C: Roll is ≤ 3 (outcomes: {1, 2, 3})
Question: What’s the probability of rolling a number that satisfies at least one of these conditions?
Solution¶
First, let’s identify the sets:
A = {2, 4, 6}, so P(A) = 3/6 = 1/2
B = {3, 6}, so P(B) = 2/6 = 1/3
C = {1, 2, 3}, so P(C) = 3/6 = 1/2
Intersections:
A ∩ B = {6} → P(A ∩ B) = 1/6
A ∩ C = {2} → P(A ∩ C) = 1/6
B ∩ C = {3} → P(B ∩ C) = 1/6
A ∩ B ∩ C = {} (empty) → P(A ∩ B ∩ C) = 0
Apply inclusion-exclusion:
Answer: The probability is 5/6.
Verification: The union is {1, 2, 3, 4, 6} — that’s 5 outcomes out of 6, confirming P = 5/6. (correct)

The General Formula¶
For n sets A₁, A₂, ..., Aₙ, the inclusion-exclusion principle extends to:
Pattern:
Add all single sets
Subtract all pairwise intersections
Add all triple intersections
Subtract all quadruple intersections
Continue alternating + and -
The signs alternate because we’re correcting for over-counting and under-counting at each step!
Key Takeaways¶
Naïve addition overcounts — elements in multiple sets get counted multiple times
Two sets:
Subtract the intersection once to fix double-counting
Three sets:
Subtract pairwise intersections, then add back the triple intersection
Intersection is commutative: (order doesn’t matter)
Works for probabilities too: Replace set sizes with probabilities
Pattern for n sets: Alternate between adding and subtracting, working through all possible intersections
Venn diagrams are your friend — they make the over-counting and correction visible!
The inclusion-exclusion principle is fundamental in combinatorics, probability theory, and computer science. Master it, and you’ll have a powerful tool for solving counting problems!