Union and Intersection Calculator
Enter the elements of two sets, separated by commas, and this calculator finds their union, intersection, set difference (A minus B and B minus A), and symmetric difference. You also get the cardinality of each result, the inclusion-exclusion formula worked out with your numbers, and a clear breakdown of which elements belong to each region of the Venn diagram. Works with numbers, letters, words, or any text tokens.
What are set union and intersection?
A set is a collection of distinct objects. Given two sets A and B, the union A ∪ B is the set of every element that appears in A, in B, or in both - it is the broadest possible combination of the two. The intersection A ∩ B is narrower: it contains only elements that appear in both sets at the same time. These two operations sit at the core of set theory and appear throughout mathematics, probability, logic, database queries (JOIN and UNION in SQL mirror them exactly), and programming (JavaScript Set.prototype.has, Python set operations).
How the five operations work
Beyond union and intersection, three further operations complete the picture. The difference A ∖ B (also written A minus B or A \ B) keeps elements of A that did not make it into B - useful when you want to remove one group from another. The reverse difference B ∖ A does the same thing from B's perspective. The symmetric difference A △ B combines both differences: it holds elements that belong to exactly one of the two sets, so everything in the overlap region drops out. Together these five operations let you describe any combination of two sets in terms of their Venn diagram regions.
The inclusion-exclusion principle and cardinality
The cardinality |S| of a set is simply the count of its elements. When you add |A| and |B|, elements in the intersection get counted twice - once in A and once in B. The inclusion-exclusion principle corrects this: |A ∪ B| = |A| + |B| - |A ∩ B|. This identity is indispensable in combinatorics and probability. For example, if 30 students study French, 20 study Spanish, and 8 study both, then the number who study at least one language is 30 + 20 - 8 = 42. The same formula extends to three or more sets, alternating additions and subtractions to avoid double- and triple-counting.
Disjoint sets, subsets, and equal sets
Two sets are disjoint when their intersection is empty - they share no elements and their Venn diagram circles do not overlap. A set A is a subset of B (written A ⊆ B) when every element of A is also in B; if A also has fewer elements than B, it is a proper subset (A ⊂ B). Two sets are equal if and only if A ⊆ B and B ⊆ A, meaning each is a subset of the other. The empty set {} is a subset of every set by convention, since there is no element in {} that could fail to be in any other set.
Set operation reference
| Operation | Notation | Definition | Empty when |
|---|---|---|---|
| Union | A ∪ B | x ∈ A or x ∈ B | Both A and B are empty |
| Intersection | A ∩ B | x ∈ A and x ∈ B | A and B are disjoint |
| Difference | A ∖ B | x ∈ A and x ∉ B | A is a subset of B |
| Reverse difference | B ∖ A | x ∈ B and x ∉ A | B is a subset of A |
| Symmetric difference | A △ B | x in exactly one set | A equals B |
Standard definitions and notation for the five operations this calculator computes.
Frequently asked questions
What is the difference between union and intersection?
The union A ∪ B collects every element from either set - the result is always at least as large as the larger of the two sets. The intersection A ∩ B keeps only elements common to both sets - the result is always at most as small as the smaller set. If the sets are disjoint (share nothing), their intersection is the empty set {} while their union is simply all elements combined.
How do I enter elements that contain commas?
This calculator splits elements on commas, semicolons, and whitespace. If your elements contain commas (for example, coordinate pairs), separate entries with semicolons or newlines instead. For example: "(1,2); (3,4); (5,6)" will correctly parse three coordinate tokens.
Can I use letters, words, or mixed content?
Yes. The calculator treats each token as a string, so sets of letters {a, b, c}, words {apple, banana, cherry}, or mixed content {1, a, hello} all work. Comparison for membership is case-insensitive, so "Apple" and "apple" count as the same element.
What does the symmetric difference mean?
The symmetric difference A △ B contains every element that is in exactly one of the two sets - not in both. You can think of it as the union minus the intersection: A △ B = (A ∪ B) ∖ (A ∩ B). It is useful in programming (detecting what changed between two lists), cryptography (XOR), and graph theory. If A equals B, the symmetric difference is empty.
Why does the inclusion-exclusion formula subtract the intersection?
When you count |A| + |B|, any element that sits in both A and B gets added twice - once for each set. Subtracting |A ∩ B| removes that double-count, giving the correct total for the union. This is the inclusion-exclusion principle. It generalises: for three sets, you add all three individual sizes, subtract all three pairwise intersections, and add back the triple intersection.
What does it mean for sets to be disjoint?
Disjoint sets have no elements in common - their intersection is the empty set. In a Venn diagram, their circles do not overlap at all. In probability, disjoint events (also called mutually exclusive events) cannot both happen at the same time, and the probability of either occurring is simply P(A) + P(B) without any overlap adjustment.