open atlas
↑ Back to track
Logic, from zero LOGIC · 03 · 02

Set operations and Venn diagrams: union, intersection, difference, complement

Union, intersection, difference, complement — four operations that are OR, AND, NOT over membership. Venn diagrams map the regions, De Morgan flips union into intersection under complement, and inclusion-exclusion explains why 1200 + 300 subscribers may be only 1400 people.

LOGIC Foundations ◷ 15 min
Level
FoundationsJuniorMiddleSenior

The ticket said: “exclude everyone who unsubscribed or whose address bounced.” The developer wrote users.filter(u => !u.unsubscribed || !u.bounced) and shipped. The next campaign emailed thousands of bounced addresses — including people who had unsubscribed years ago — and the sender domain landed on a spam blocklist. Read the condition slowly: a user who bounced but never unsubscribed has !u.unsubscribed equal to true, so the OR lets them through. The author wanted “outside the union of two groups” and wrote “outside one group, or outside the other” — and those are different regions. There is a 170-year-old law that says exactly how negation distributes over a union, and a diagram with two overlapping circles on which the bug is visible at a glance.

Goal

After this lesson you can state the membership rule for each of the four set operations, draw and read a two-circle Venn diagram, apply De Morgan’s law to a filter condition, use inclusion-exclusion to count a union, and recognize that set difference is directional.

1

There are four classic operations that combine sets, each defined by a membership rule. Take A = {1, 2, 3} and B = {3, 4}:

  • Union A ∪ B — members in A or in B (or both): {1, 2, 3, 4}.
  • Intersection A ∩ B — members in A and in B: {3}.
  • Difference A ∖ B — members of A that are not in B: {1, 2}. The direction matters: B ∖ A is {4}, a different set.
  • Complement — everything not in A, within a declared universe U. Without U, “everything” is undefined and the complement is meaningless.

These are just OR, AND, AND-NOT and NOT applied to membership statements. Every law from propositional logic — above all De Morgan — transfers verbatim.

a = {1, 2, 3}
b = {3, 4}

a | b   # union        → {1, 2, 3, 4}
a & b   # intersection → {3}
a - b   # difference   → {1, 2}
b - a   # other direction → {4}
2

A Venn diagram draws each set as a circle inside a rectangle (the universe). Two overlapping circles cut the rectangle into exactly four regions: A only, both, B only, neither. Each operation is a selection of regions — union shades three, intersection shades the middle lens, difference A ∖ B shades the left crescent, and the complement of A ∪ B shades only the outside. Read a Venn diagram like a map: not “what does ∩ mean?” but “which regions are shaded?”

3

De Morgan’s law transfers verbatim from logic to sets. The complement of A ∪ B equals the intersection of the complements: not in (A ∪ B) means not in A AND not in B. The buggy filter wanted the complement of (unsubscribed ∪ bounced):

// Wanted: NOT (unsubscribed OR bounced) = De Morgan = (NOT unsubscribed) AND (NOT bounced)
const eligible = users.filter((u) => !u.unsubscribed && !u.bounced); // correct

// Shipped: (NOT unsubscribed) OR (NOT bounced) — three Venn regions instead of one
const buggy = users.filter((u) => !u.unsubscribed || !u.bounced);
4

Counting a union uses the inclusion-exclusion principle. Adding |A| + |B| counts the lens twice — once per circle — so subtract the overlap once: |A ∪ B| = |A| + |B| − |A ∩ B|. Newsletter 1,200, beta 300, overlap 100: 1,200 + 300 − 100 = 1,400, not 1,500.

Worked example

Users: A = active, B = beta. Universe U = all registered accounts.

Use the membership rules to classify each filter.

  • users.filter(u => isActive(u) && isBeta(u)) — A ∩ B: users in BOTH groups (the lens).
  • users.filter(u => isActive(u) || isBeta(u)) — A ∪ B: users in EITHER group (three regions).
  • users.filter(u => isActive(u) && !isBeta(u)) — A ∖ B: active users who are NOT beta (left crescent).
  • users.filter(u => !isActive(u) && !isBeta(u)) — complement(A ∪ B): neither active nor beta (outside region).

De Morgan check on the last filter: NOT (active OR beta) = (NOT active) AND (NOT beta) — confirmed, the && is correct.

Common mistake

Reliable De Morgan check in review: test the suspicious condition against one concrete member of each Venn region — a user who is only in one bad set, one who is in both, one who is in neither. Four test values, four regions — a wrong connective always misclassifies at least one. Faster and more convincing than re-deriving the algebra in your head.

Practice 0 / 5

A = {1, 2, 3}, B = {3, 4}. What is A ∪ B? List members inside braces.

A = {1, 2, 3}, B = {3, 4}. What is A ∩ B? List members inside braces.

A = {1, 2, 3}, B = {3, 4}. What is A ∖ B? List members inside braces.

Newsletter has 500 subscribers, beta has 200 members, 50 are in both. What is |newsletter ∪ beta|?

The spec says: NOT (unsubscribed OR bounced). Which filter is correct? Type A or B. A: !u.unsubscribed && !u.bounced B: !u.unsubscribed || !u.bounced

Check yourself
Quiz

Within universe U, what is the complement of A ∪ B?

Recap

Four operations combine sets, and each is a logic connective acting on membership: union A ∪ B is OR, intersection A ∩ B is AND, difference A ∖ B is AND-NOT, and the complement is NOT — legal only inside a declared universe U. Because the operations are connectives, De Morgan transfers verbatim: the complement of A ∪ B is the intersection of the complements. A Venn diagram makes each operation a region you can point at — two circles cut the universe into four regions, and every value lives in exactly one. Counting follows: |A ∪ B| = |A| + |B| − |A ∩ B|, because summing circle sizes counts the lens twice. And difference is directional: A ∖ B and B ∖ A answer different questions.

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.

recallapplystretch0 of 5 done

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.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources4
expand
  1. 01
  2. 02
  3. 03
  4. 04

Trademarks belong to their respective owners. Editorial reference only.