Set Operations Calculator (Union, Intersection, Complement)

Type two sets and this calculator returns every standard operation on them at once: union, intersection, both differences, the symmetric difference and - if you supply a universal set - both complements, each with its cardinality. It also reports the size of the power set (2|A|) and of the Cartesian product (|A|·|B|), checks the inclusion-exclusion identity, and tells you whether the sets are disjoint, nested or equal. Duplicates are removed automatically, because a set records presence rather than count.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Set AElements separated by commas. Numbers or words both work; duplicates are collapsed.1, 2, 3, 4, 5
Set BThe second set, entered the same way.4, 5, 6, 7
Operation to featureChooses which result is shown as the headline. Every other operation still appears in the table.A ∪ B (union)
Universal set U (optional)Needed only for complements. Leave blank if you are not taking one.1, 2, 3, 4, 5, 6, 7, 8, 9, 10

It returns

  • Result of the chosen operation — The empty set is written ∅.
  • Cardinality of the result
  • |A|
  • |B|
  • |A ∪ B|
  • |A ∩ B|
  • Subsets of A (2^|A|)
  • Ordered pairs in A × B

The formula

|AB|=|A|+|B||AB|
|P(A)|=2|A|

In plain text: |A ∪ B| = |A| + |B| − |A ∩ B|

  • A ∪ BUnion — elements in A or B or both
  • A ∩ BIntersection — elements in both
  • A − BDifference — in A but not in B
  • A Δ BSymmetric difference — in exactly one of the two
  • A′Complement — in the universal set U but not in A

Sets contain distinct, unordered elements. Duplicates carry no information and are removed on entry.

Updated Category Discrete Math, Logic & Graph Theory Verified against published test cases Reading time 11 min

The operations, and what each one asks

A set is an unordered collection of distinct things. Two properties follow immediately and both trip people up: {1, 2, 2, 3} is the same set as {1, 2, 3}, because repetition carries no information, and {3, 1, 2} is the same set again, because order carries none either. This calculator therefore removes duplicates on input and displays results in sorted order purely for readability.

The operations answer different membership questions about an element x:

  • Union A ∪ B - is x in A or in B (or both)?
  • Intersection A ∩ B - is x in A and in B?
  • Difference A − B - is x in A but not in B?
  • Symmetric difference A Δ B - is x in exactly one of them?
  • Complement A′ - is x in the universe but not in A?

Read that list again as logic and the correspondence is exact: union is OR, intersection is AND, complement is NOT, and symmetric difference is exclusive OR. That is not an analogy - set algebra and Boolean algebra are the same structure with different notation, and every identity in one has a twin in the other.

Two further constructions change the type of the answer rather than filtering it. The power set P(A) is the set of all subsets of A, so its members are sets rather than elements. The Cartesian product A × B is the set of all ordered pairs with the first entry from A and the second from B - and because the pairs are ordered, A × B and B × A are different sets whenever A and B differ.

Counting: inclusion-exclusion and the two size rules

The cardinality of a union is not the sum of the cardinalities, because elements in both sets would be counted twice. Subtracting the overlap once fixes it:

|A ∪ B| = |A| + |B| − |A ∩ B|

For three sets the pattern continues, adding back what has been over-subtracted:

|A ∪ B ∪ C| = |A| + |B| + |C| − |A∩B| − |A∩C| − |B∩C| + |A∩B∩C|

The alternating signs are the whole content of the inclusion-exclusion principle, and it generalises to any number of sets with the same alternation. It is the counting tool behind derangement problems, the sieve of Eratosthenes, and the probability rule P(A or B) = P(A) + P(B) − P(A and B), which is the identical statement with measure in place of count.

Two more counting rules are worth knowing cold. The power set has |P(A)| = 2|A| members, because building a subset means making an independent in-or-out decision for each of the |A| elements. And the Cartesian product has |A × B| = |A| · |B| members, one for each way of choosing a first entry and then a second.

Notice the boundary cases those formulas handle correctly. The empty set has 20 = 1 subset - itself - so P(∅) = {∅}, a set with one member, not an empty set. And ∅ × B is empty for any B, since there is no first entry to choose.

The complement is the only operation that needs outside information. A′ is meaningless until you say what the universe is: the complement of the even numbers is the odd numbers within the integers, but something quite different within the reals. That is why the universal set is a separate input here, and why the calculator refuses to compute a complement without one.

Worked example: A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}

Take the universe to be U = {1, 2, …, 10}.

  1. Union. Everything in either set: A ∪ B = {1, 2, 3, 4, 5, 6, 7}, seven elements.
  2. Intersection. Only what appears in both: A ∩ B = {4, 5}, two elements.
  3. Check inclusion-exclusion. |A| + |B| − |A ∩ B| = 5 + 4 − 2 = 7. That matches the union count, which is the arithmetic check worth doing every time.
  4. Differences. A − B = {1, 2, 3} and B − A = {6, 7}. These are different sets - set difference is not commutative.
  5. Symmetric difference. The two differences combined: A Δ B = {1, 2, 3, 6, 7}, five elements. Equivalently |A ∪ B| − |A ∩ B| = 7 − 2 = 5.
  6. Complements. A′ = {6, 7, 8, 9, 10} and B′ = {1, 2, 3, 8, 9, 10}.
  7. De Morgan check. (A ∪ B)′ = {8, 9, 10}. And A′ ∩ B′ - the elements in both complements - is also {8, 9, 10}. The law holds.
  8. Counts. The power set of A has 25 = 32 subsets, and A × B has 5 × 4 = 20 ordered pairs.

One warning about that last line. A × B has 20 members; B × A also has 20, but they are not the same 20. (1, 4) belongs to the first and (4, 1) to the second, and the two products share no member at all unless A and B overlap.

Reading the relationships

Three relationships between two sets matter more than the rest, and the calculator reports each when it holds.

Disjoint means the intersection is empty - the sets share nothing. Then |A ∪ B| collapses to |A| + |B| with no correction term, and in probability, disjoint events cannot both occur, so their probabilities simply add.

Subset means every element of one lies in the other. When A ⊆ B, the union is B and the intersection is A - the two operations degenerate. Every set is a subset of itself, and the empty set is a subset of every set, which is why ∅ ⊆ A is always true and not a special case.

Equal means each is a subset of the other. That is the standard way to prove two set expressions describe the same set: show containment in both directions. It is worth noticing that equality has nothing to do with the order you typed the elements in.

Watch the cardinality of the symmetric difference as a distance. |A Δ B| counts the elements the two sets disagree about, and it behaves like a genuine metric: zero exactly when the sets are equal, symmetric in A and B, and obeying the triangle inequality. Divide it by |A ∪ B| and you have the Jaccard distance used to compare documents, gene sets and search results.

Set identities worth memorising

Each identity holds for all sets. The Boolean column shows the same law in logic notation.
NameSet formBoolean form
CommutativeA ∪ B = B ∪ Aa + b = b + a
Associative(A ∪ B) ∪ C = A ∪ (B ∪ C)(a + b) + c = a + (b + c)
DistributiveA ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)a(b + c) = ab + ac
De Morgan (union)(A ∪ B)′ = A′ ∩ B′(a + b)′ = a′b′
De Morgan (intersection)(A ∩ B)′ = A′ ∪ B′(ab)′ = a′ + b′
IdempotentA ∪ A = Aa + a = a
AbsorptionA ∪ (A ∩ B) = Aa + ab = a
Double complement(A′)′ = A(a′)′ = a
Difference as intersectionA − B = A ∩ B′ab′
Symmetric differenceA Δ B = (A − B) ∪ (B − A)a ⊕ b = ab′ + a′b

The Boolean column uses + for OR, juxtaposition for AND and a prime for NOT. Every row can be verified on this page by entering sets that make both sides computable.

Where people go wrong

  • Treating a set like a list. Duplicates vanish and order is meaningless. If you need repetition, you want a multiset or bag; if you need order, you want a sequence or tuple.
  • Assuming set difference is commutative. A − B and B − A are different sets, and they are equal only when both are empty - that is, only when A = B.
  • Taking a complement without a universe. A′ is undefined until the universal set is stated, and it changes completely when the universe changes.
  • Confusing ∈ with ⊆. For A = {1, 2}, we have 1 ∈ A and {1} ⊆ A, but 1 ⊆ A and {1} ∈ A are both false. Elements and subsets are different kinds of thing - and the distinction matters most inside power sets, where the members are themselves sets.
  • Writing the empty set as {∅}. Those differ: ∅ has no members and {∅} has exactly one, namely the empty set. |∅| = 0 and |{∅}| = 1.
  • Adding cardinalities without subtracting the overlap. |A| + |B| overcounts every shared element exactly once, which is what inclusion-exclusion corrects.

Why these operations turn up everywhere

Relational databases are set theory with a query language on top. SQL's UNION, INTERSECT and EXCEPT are exactly the three operations above - with one important difference: SQL tables are multisets by default, so UNION ALL keeps duplicates while plain UNION removes them and behaves like the set operation. A CROSS JOIN is the Cartesian product, and its row count is the product of the two table sizes, which is why an accidental cross join is so catastrophic on large tables.

Probability is measure theory on sets. Events are subsets of a sample space, P(A ∪ B) = P(A) + P(B) − P(A ∩ B) is inclusion-exclusion, mutually exclusive means disjoint, and the complement rule P(A′) = 1 − P(A) is the complement operation with the universe being the whole sample space.

In combinatorics, the power-set formula 2n is the reason a set of n elements has that many subsets, and splitting the count by subset size gives the binomial coefficients, since the number of k-element subsets is exactly n choose k. Summing those coefficients over all k returns 2n, which is the combinatorial proof of the power-set rule.

In digital logic, sets over a universe of n elements are bit vectors of length n, and the operations become bitwise AND, OR, XOR and NOT - which is why set operations on small universes are among the fastest things a processor does. The same correspondence lets you carry any identity from the table above straight into a truth table or a Karnaugh map, and back again.

Notation

∈
Membership: 3 ∈ {1, 2, 3} reads ‘3 is an element of the set’.
⊆
Subset: every element of the left set is in the right set. Every set is a subset of itself.
∅
The empty set: the unique set with no members. Its cardinality is 0 and it is a subset of every set.
|A|
Cardinality: the number of distinct elements in A.
P(A)
Power set: the set of all subsets of A, with 2|A| members.
A × B
Cartesian product: all ordered pairs (a, b) with a in A and b in B.

Frequently asked questions

Does the order I type the elements in matter?

No. A set is unordered, so {3, 1, 2} and {1, 2, 3} are the same set and every operation returns the same result. The calculator sorts the output - numerically when every element is a number, alphabetically otherwise - purely so the results are easy to read and compare. If order matters for your problem, you need a sequence or tuple rather than a set.

What happens to duplicate elements?

They are collapsed to a single occurrence, because a set records only whether an element is present. Entering 1, 1, 2, 2, 3 gives a set of cardinality 3. If you need to count repetitions you are working with a multiset, and set operations do not apply to it unchanged - the union of multisets, for instance, has two sensible definitions depending on whether you take the maximum or the sum of the multiplicities.

Why do I need a universal set for the complement?

Because the complement of A is everything not in A, and that phrase is empty until you say what ‘everything’ means. The complement of the even numbers is the odd numbers if the universe is the integers, but a much larger set if the universe is the reals. All the other operations on this page are defined without a universe, which is why it is an optional input.

What is the difference between A − B and A Δ B?

A − B keeps only what is in A and not in B, so it is one-sided and not commutative. A Δ B keeps what is in exactly one of them, which is (A − B) together with (B − A), and it is commutative. For A = {1,2,3,4,5} and B = {4,5,6,7}, the difference is {1,2,3} and the symmetric difference is {1,2,3,6,7}.

How large can the power set get?

It doubles with every extra element: 10 elements give 1,024 subsets, 20 give 1,048,576, and 64 give more than 10¹⁹. This calculator lists the subsets only when A has five or fewer elements and reports the count alone beyond that. The growth is also why exhaustive subset search is impractical for anything but small sets.

Is A × B the same as B × A?

No, unless A and B are equal or one is empty. The pairs are ordered, so (1, 4) and (4, 1) are different members. The two products have the same cardinality, |A|·|B|, but they generally share no member at all. This asymmetry is exactly why a Cartesian product is used to build coordinates, where the first and second slots mean different things.

How do these relate to SQL?

Directly. UNION, INTERSECT and EXCEPT are the set union, intersection and difference, and CROSS JOIN is the Cartesian product. The one gap is duplicates: SQL result sets are multisets, so UNION ALL keeps repeats while UNION removes them. If you are checking a query against set algebra, use the duplicate-removing forms or the identities will not hold.

Can I use words and letters instead of numbers?

Yes. Elements are compared as text after trimming spaces, so apple, banana, cherry works exactly like a numeric set, and the results are sorted alphabetically instead of numerically. Separate the elements with commas when they contain spaces; if there is no comma anywhere in the field, the calculator falls back to splitting on whitespace.

References

  • Discrete Mathematics and Its Applications, 8th ed. (Chapter 2, Basic Structures: Sets, Functions, Sequences, Sums) — Kenneth H. Rosen, McGraw-Hill
  • Naive Set Theory — Paul R. Halmos, Springer (Undergraduate Texts in Mathematics)
  • Concrete Mathematics: A Foundation for Computer Science, 2nd ed. — Ronald L. Graham, Donald E. Knuth and Oren Patashnik, Addison-Wesley