Formula Bro

Math and physics formulas, explained like you are new.


Inclusion-Exclusion Principle

In one sentence: Inclusion-exclusion counts how many items belong to at least one group by adding the groups and then removing the overlaps that were counted twice.

|A∪B|=|A|+|B|−|A∩B||A \cup B| = |A| + |B| - |A \cap B|

Read it out loud: “The size of A union B equals the size of A, plus the size of B, minus the size of A intersect B. The vertical bars mean count how many items are in the set. The union sign means either set or both, and the intersection sign means the items that are in both sets.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Sets and their members

A set is a collection of items, such as the members of a chess club. Each item in a set is called a member or an element, and each member is counted once. The capital letters AA and BB are names for sets. If a chess club has 30 members, then set AA has 30 members.

Vertical bars, |A||A|

|A||A| is read ‘the size of A’, and it means the number of items in set AA. For a club with 30 members, |A|=30|A| = 30. In this formula the bars mean count, not absolute value, which is a different use of the same symbol that you may see elsewhere.

Union ∪\cup and intersection ∩\cap

A∪BA \cup B, the union, holds every item that is in AA or in BB, or in both. It means ‘or’. A∩BA \cap B, the intersection, holds only the items that are in both AA and BB. It means ‘and’. The union is the total of the two groups, and the intersection is their shared part.

Venn diagrams

A Venn diagram draws each set as a circle. Two overlapping circles labeled AA and BB have three regions: items inside only AA, items inside only BB, and items in the shared overlap. Items outside both circles are in neither set. Drawing a Venn diagram shows which items are counted twice.

Double counting

Double counting happens when one item is added to a total more than once. If a student belongs to both the chess club and the choir, adding the two club sizes counts that student twice. Inclusion-exclusion is the method for fixing this mistake.

What each symbol means

SymbolMeaning

|A∪B||A \cup B|

The number of items in either set AA or set BB, or in both, with each item counted once. The vertical bars mean the count of the set. The result is a plain count of people, items or cards, so it has no unit.

|A||A|

The number of items in set AA, a whole number such as 30 for a chess club with 30 members. The bars mean count. It has no unit, because it counts people or things.

|B||B|

The number of items in set BB, a whole number such as 25 for a choir with 25 singers. It counts members, so it has no unit, and it is read ‘the size of B’.

|A∩B||A \cap B|

The number of items that are in both AA and BB, such as 10 students who are in both the chess club and the choir. Those items were counted in |A||A| and again in |B||B|, so they are subtracted once. It has no unit.

The formula piece by piece

|A||A|

The count of set AA alone. For the chess club, |A|=30|A| = 30 students. This is the first count that goes into the total, and every member of AA is included in it.

|B||B|

The count of set BB alone. For the choir, |B|=25|B| = 25 students. Adding this count to the first one gives a total where any student in both clubs is counted twice.

|A∩B||A \cap B|

The count of the overlap, the items that are in both sets. Those items were added once in |A||A| and once in |B||B|, so they appear twice in the sum. In the school example, 10 students are in both the chess club and the choir.

|A∪B|=|A|+|B|−|A∩B||A \cup B| = |A| + |B| - |A \cap B|

In one plain sentence: the number of items in either set AA or set BB, with each item counted once, equals the size of AA plus the size of BB minus the size of the overlap between them.

|A∪B∪C|=|A|+|B|+|C|−|A∩B|−|A∩C|−|B∩C|+|A∩B∩C||A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

The same rule for three sets, a standard form to know. Add the three single counts, subtract the three pairwise overlaps, and then add back the triple overlap, which is the set of items that belong to all three sets.

What the formula is doing, and why

What goes in and what comes out. You put in the sizes of the sets and the size of their overlap. You get out the number of items that are in at least one set, with each item counted exactly once. The bars mean count, and the answer has no unit, because it counts people, items or cards.

Why subtract the overlap? Adding |A||A| and |B||B| counts every item in both sets twice, once for each set. So the overlap |A∩B||A \cap B| is subtracted once to fix that. After the subtraction, each shared item is counted exactly one time. The result is the number of items in AA or BB.

Three sets. The same idea works for three sets: |A∪B∪C|=|A|+|B|+|C|−|A∩B|−|A∩C|−|B∩C|+|A∩B∩C||A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| Adding the three sizes counts each item once for every set it is in. Subtracting the three pairwise overlaps removes the extra counts. But an item in all three sets was added 3 times and subtracted 3 times, so it now counts as zero. The triple overlap is added back once to fix that.

The signs alternate. Add the single sets, subtract the pairs, add the triples, and so on for more sets. Each extra set adds more overlaps to count, so the method is most useful when the overlaps are easy to count.

When should you use it? Use it for questions that ask how many items have at least one property. Counting each property on its own and then correcting the overlaps is often easier than counting the mixed cases one at a time. If the sets do not overlap, the overlap is zero and the formula is the plain sum of the two counts.

An everyday picture

Think of two guest lists for a party, one for a chess club and one for a choir. Some people are on both lists. Adding the two list lengths counts those people twice. Subtract the people who are on both lists, and you have the number of different people who came from at least one list.

Worked example: Students in three school activities

A school has 60 students. Let AA be the chess club with 30 students, BB the choir with 25 students, and CC the robotics team with 20 students. The overlaps are: 10 students are in both AA and BB, 8 are in both AA and CC, 6 are in both BB and CC, and 4 are in all three. We want |A∪B∪C||A \cup B \cup C|, the number of students in at least one activity.

  1. Add the three single counts. Add the sizes of the three activities: |A|+|B|+|C|=30+25+20=75|A| + |B| + |C| = 30 + 25 + 20 = 75. This counts every student once for each activity they belong to, so the 4 students in all three are counted three times.
  2. Subtract the pairwise overlaps. The overlaps for pairs are 10, 8 and 6, and together they add to 24. Subtracting them gives 75−24=5175 - 24 = 51. This removes one count from each overlap, so a student in exactly two activities now counts once. A student in all three has been removed three times and now counts zero times.
  3. Add back the triple overlap. The 4 students in all three activities now count zero times, so add them back once: 51+4=5551 + 4 = 55. So |A∪B∪C|=55|A \cup B \cup C| = 55, the number of students in at least one activity.
  4. Find the students in no activity. The school has 60 students, and 55 of them are in at least one activity. So 60−55=560 - 55 = 5 students are in none of the three activities.
  5. Check by counting the regions. Students in all three: 4. In exactly two activities: chess and choir only 10−4=610 - 4 = 6, chess and robotics only 8−4=48 - 4 = 4, choir and robotics only 6−4=26 - 4 = 2. In exactly one activity: chess only 30−(6+4+4)=1630 - (6 + 4 + 4) = 16, choir only 25−(6+4+2)=1325 - (6 + 4 + 2) = 13, robotics only 20−(4+2+4)=1020 - (4 + 2 + 4) = 10. Adding all seven regions gives 16+13+10+6+4+2+4=5516 + 13 + 10 + 6 + 4 + 2 + 4 = 55, which matches.

Answer: 55 students are in at least one activity, and 5 students are in none. The 4 students in all three activities are counted exactly once in the final total.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why do we subtract the overlap instead of counting each set once?

Counting each set once gives the wrong total when sets share members. An item in both sets gets added twice, once for each set. Subtracting the overlap once leaves each item counted exactly one time.

Does the formula always subtract?

For two sets, the overlap is always subtracted. For three sets the signs alternate: add the singles, subtract the pairs, then add back the triple overlap. The same pattern continues for more sets.

What happens when the sets do not overlap?

Then |A∩B|=0|A \cap B| = 0, the overlap is empty, and the formula reduces to |A|+|B||A| + |B|, the ordinary sum. For example, a chess club of 30 and a choir of 25 with no shared members gives 55 people in total.

Sources

  1. Inclusion-exclusion principle, Wikipedia
  2. Union (set theory), Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/inclusion-exclusion-principle.md.