Set operations calculator

Union, intersection, differences and symmetric difference of two sets of numbers or words, with the Jaccard index and a Venn diagram.

Drafted with AI writing toolsUpdated Checked examples: 6

Separate items with commas, semicolons or new lines (or spaces if you use none of those). Numbers like 1 and 1.0 count as the same item.
Try
Union A ∪ B
{1, 2, 3, 4, 5, 6, 7}
Union A ∪ B: {1, 2, 3, 4, 5, 6, 7}
Intersection A ∩ B
{4, 5}
Difference A − B
{1, 2, 3}
Difference B − A
{6, 7}
Symmetric difference A △ B
{1, 2, 3, 6, 7}
|A|
5
|B|
4
|A ∪ B|
7
|A ∩ B|
2
Jaccard index |A ∩ B| / |A ∪ B|
0.285714

A and B share 2 of the 7 distinct items (Jaccard index 0.2857); 3 are only in A and 2 only in B.

Venn diagram (counts below each region)

AB1233452672
How it's calculated S
  1. The two sets

    A={1,2,3,4,5}, ∣A∣=5;B={4,5,6,7}, ∣B∣=4A = \{1, 2, 3, 4, 5\},\ |A| = 5;\qquad B = \{4, 5, 6, 7\},\ |B| = 4
  2. Union: in A or in B

    A∪B={1,2,3,4,5,6,7}A \cup B = \{1, 2, 3, 4, 5, 6, 7\}
  3. Intersection: in both

    A∩B={4,5}A \cap B = \{4, 5\}
  4. Differences: in one but not the other

    A∖B={1,2,3},B∖A={6,7}A \setminus B = \{1, 2, 3\},\qquad B \setminus A = \{6, 7\}
  5. Symmetric difference: in exactly one

    A△B=(A∖B)∪(B∖A)={1,2,3,6,7}A \mathbin{\triangle} B = (A \setminus B) \cup (B \setminus A) = \{1, 2, 3, 6, 7\}
  6. Count check (inclusion–exclusion)

    ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣=5+4−2=7|A \cup B| = |A| + |B| - |A \cap B| = 5 + 4 - 2 = 7

    Jaccard index: 2 / 7 = 0.285714.

About the set operations calculator

The calculator treats each list as a set, dropping repeats, and compares the two. The union A ∪ B holds everything in either set, the intersection A ∩ B what is in both, the differences A − B and B − A what is in one but not the other, and the symmetric difference A △ B what is in exactly one. The Jaccard index |A ∩ B| ÷ |A ∪ B| scores the overlap from 0 for disjoint sets to 1 for identical ones.

Comparing customer or email lists, finding shared tags, checking what one version of a list adds or removes, and discrete-mathematics homework are typical uses. The default sets {1, 2, 3, 4, 5} and {4, 5, 6, 7} share 2 of their 7 distinct items, so the Jaccard index is 2/7 ≈ 0.2857.

Items can be numbers or words, separated by commas, semicolons, new lines or spaces. Numbers are compared by value, so 1 and 1.0 are one item; words must match exactly, including capital letters. Results list numbers in order, then words alphabetically.

Union, intersection and difference of two sets

The union A ∪ B holds everything in either set, the intersection A ∩ B only what both share, and the difference A − B what A has that B lacks. For A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}, A ∪ B = {1, 2, 3, 4, 5, 6, 7}, A ∩ B = {4, 5} and A − B = {1, 2, 3}.

Each element counts once, so 4 and 5 appear only once in the union even though both sets contain them.

Set operations at a glance

These results use the sets above, A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}.

OperationMeaningResult for the default sets
A ∪ BIn either set or both{1, 2, 3, 4, 5, 6, 7}
A ∩ BIn both sets{4, 5}
A − BIn A but not B{1, 2, 3}
B − AIn B but not A{6, 7}
A △ BIn exactly one set{1, 2, 3, 6, 7}

Union, intersection and symmetric difference give the same answer in either order. The two differences do not: A − B = {1, 2, 3}, while B − A = {6, 7}. A − B is also written A \ B.

Symmetric difference

A △ B holds the elements that are in exactly one of the sets. It equals (A − B) ∪ (B − A), or the union with the intersection removed: {1, 2, 3, 6, 7} here.

The symmetric difference is the set version of exclusive or (XOR), and Python's ^ operator computes it (Python set documentation).

Counting elements: inclusion–exclusion

The size of a union is |A ∪ B| = |A| + |B| − |A ∩ B|, because elements in both sets would otherwise be counted twice. For the sets above, 5 + 4 − 2 = 7.

Word problems use the same rule. If 18 of 30 students play football, 12 play cricket and 5 play both, then 18 + 12 − 5 = 25 play at least one of the two, and 30 − 25 = 5 play neither.

For three sets the rule extends to |A ∪ B ∪ C| = |A| + |B| + |C| − |A ∩ B| − |A ∩ C| − |B ∩ C| + |A ∩ B ∩ C|.

Jaccard index

The Jaccard index measures overlap as |A ∩ B| ÷ |A ∪ B|, from 0 for sets with nothing in common to 1 for identical sets. For the sets above it is 2/7 ≈ 0.2857, and the Jaccard distance is 1 − 2/7 = 5/7 ≈ 0.714.

It counts distinct elements only, so it ignores how often an item was listed and treats near-spellings as different items.

Subsets, complements and the empty set

If A − B is empty, every element of A is also in B, so A is a subset of B (A ⊆ B).

A complement needs a universal set U: A′ = U − A. With U = {1, 2, …, 10} and A = {1, 2, 3, 4, 5}, A′ = {6, 7, 8, 9, 10}. B − A is the complement of A only when B is the whole universe.

The empty set ∅ has no elements: ∅ ∪ B = B and ∅ ∩ B = ∅. When both sets are empty, the Jaccard index would be 0 ÷ 0, so it is undefined.

Common mistakes

  • Counting shared elements twice. The union of {1, 2} and {2, 3} is {1, 2, 3}, with three elements, not four.
  • Treating A − B and B − A as the same. They never share an element.
  • Keeping duplicates. A set holds each element once, so 1, 1.0 and 2 form a set of two elements.
  • Ignoring capital letters. Apple and apple are different text elements.

Input format

  • Separate items with commas, semicolons or new lines. Spaces separate items only when none of those appear, so use commas to keep New York as one item.
  • Numbers compare by value: 1, 1.0 and 1e0 are one element. A fraction such as 1/2 is read as text, so write 0.5 to compare it with numbers.
  • Leave a box empty for the empty set. Results list numbers in numerical order, then words alphabetically.

For and, or and xor on true/false variables, use the truth table generator. For the probability version of inclusion–exclusion, P(A or B) = P(A) + P(B) − P(A and B), use the probability calculator.

Worked examples

{1, 2, 3, 4, 5} and {4, 5, 6, 7}

Set A
1, 2, 3, 4, 5
Set B
4, 5, 6, 7
Union A ∪ B
{1, 2, 3, 4, 5, 6, 7}
Intersection A ∩ B
{4, 5}
Difference A − B
{1, 2, 3}
Difference B − A
{6, 7}
Symmetric difference A △ B
{1, 2, 3, 6, 7}
|A ∪ B|
7
Jaccard index |A ∩ B| / |A ∪ B|
0.285714

Checked against: Set operators |, &, -, ^ on {1,2,3,4,5} and {4,5,6,7}; 2/7 = 0.285714

Words with one item in common

Set A
apple, banana, cherry
Set B
banana, date
Union A ∪ B
{apple, banana, cherry, date}
Intersection A ∩ B
{banana}
Symmetric difference A △ B
{apple, cherry, date}
Jaccard index |A ∩ B| / |A ∪ B|
0.25

Checked against: Independent calculation

Disjoint sets (edge case)

Set A
1, 3, 5
Set B
2, 4, 6
Intersection A ∩ B
∅
|A ∩ B|
0
Union A ∪ B
{1, 2, 3, 4, 5, 6}
Jaccard index |A ∩ B| / |A ∪ B|
0

Checked against: {1,3,5} & {2,4,6} == set()

Equal numbers written differently

Set A
1, 1.0, 2
Set B
2.00, 3
|A|
2
Intersection A ∩ B
{2}
Union A ∪ B
{1, 2, 3}

Checked against: Independent calculation

Questions

What is the difference between union and intersection?

The union A ∪ B contains every element in A, in B or in both; the intersection A ∩ B contains only the elements in both. For A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}, the union is {1, 2, 3, 4, 5, 6, 7} and the intersection is {4, 5}. The union is never smaller than either set, and the intersection never larger.

What is the symmetric difference of two sets?

The elements that are in exactly one of the two sets: A △ B = (A − B) ∪ (B − A), which is the union with the intersection removed. For {1, 2, 3, 4, 5} and {4, 5, 6, 7} it is {1, 2, 3, 6, 7}. It behaves like XOR in logic, and Python's ^ operator computes it for sets.

How do you find the number of elements in a union?

Use the inclusion–exclusion principle: |A ∪ B| = |A| + |B| − |A ∩ B|, subtracting the intersection because its elements were counted twice. With |A| = 5, |B| = 4 and |A ∩ B| = 2, the union has 5 + 4 − 2 = 7 elements. For three sets it extends to |A| + |B| + |C| − |A ∩ B| − |A ∩ C| − |B ∩ C| + |A ∩ B ∩ C|.

What is the Jaccard index?

A similarity score for two sets: the size of their intersection divided by the size of their union, from 0 for disjoint sets to 1 for identical ones. {1, 2, 3, 4, 5} and {4, 5, 6, 7} share 2 of 7 distinct items, a Jaccard index of 2/7 ≈ 0.2857. It is used to compare documents, tag sets and search results, and 1 minus the index is the Jaccard distance.

Is A − B the same as B − A?

No. A − B, also written A \ B, keeps the elements of A that are not in B, while B − A keeps the elements of B that are not in A. With A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}, A − B = {1, 2, 3} but B − A = {6, 7}. The two differences never share an element, and together they form the symmetric difference.

About this calculator

A∪B={x:x∈A∨x∈B}A∩B={x:x∈A∧x∈B}A△B=(A∖B)∪(B∖A)\begin{gathered} A \cup B = \{x : x \in A \lor x \in B\} \\[6pt] A \cap B = \{x : x \in A \land x \in B\} \\[6pt] A \mathbin{\triangle} B = (A \setminus B) \cup (B \setminus A) \end{gathered}

Sources

  1. Wolfram MathWorld — Union, Intersection, Set Difference, Symmetric Difference
  2. Python documentation — set types and operations

Checked against references

This calculator includes 6 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.

Put this calculator on your site

Paste this where you want the calculator to appear. It is free to use, it stays in step with corrections made here, and the credit line below it links back so your readers can find the full version.

Height
Preview the embed

Keep the credit line. It is the only condition, and it is what keeps these calculators free.