Set Operations Calculator – Union, Intersection, Difference & Complement of Sets and Intervals
Choose a mode: sets listed element by element (with a Venn diagram) or intervals and rays on the real line ℝ (with shaded number lines). Step-by-step solutions follow the grade 10 curriculum.
The calculator above performs the basic operations of set theory on two sets of real numbers: union, intersection, set difference (relative complement) and complement. It has two modes:
- Discrete Elements & Venn: enter the elements of two finite sets in roster form (integers, decimals or fractions). The tool removes duplicates, sorts each set, draws a Venn diagram and writes a five-step solution.
- Intervals & Number Line: enter subsets of the real line in interval notation, such as [−4, 1] or [1, +∞), and pick A ∩ B, A ∪ B, A \ B, B \ A, Aᶜ or Bᶜ. The tool plots three shaded number lines and explains the answer in four steps.
Sets, elements and roster notation
A set is a well-defined collection of distinct objects called elements. We write a ∈ A when a is an element of A and a ∉ A when it is not. A set can be described in two ways:
- Roster notation lists the elements between braces, for example A = {−3, 0, 1.5, 2}. Each element is listed once, and order does not matter.
- Set-builder notation states a defining property, for example B = {x ∈ ℤ | −2 ≤ x < 3}.
The set with no elements is the empty set ∅. The number of elements of a finite set A is its cardinality, written n(A) or |A|.
Subsets and equal sets
A is a subset of B, written A ⊆ B, when every element of A is also an element of B:
Two sets are equal when A ⊆ B and B ⊆ A. For every set A, ∅ ⊆ A and A ⊆ A.
Basic set operations
Intersection (A ∩ B)
The intersection of A and B is the set of elements that belong to both A and B:
If A ∩ B = ∅, the sets are disjoint.
Union (A ∪ B)
The union of A and B is the set of elements that belong to A or to B (or to both). Common elements are written once:
Set difference / relative complement (A \ B)
The difference A \ B, also called the relative complement of B in A, is the set of elements of A that are not in B. Some textbooks write it A − B:
Set difference is not commutative: in general A \ B ≠ B \ A.
Complement
All sets in a problem are usually taken inside a universal set U. The (absolute) complement of A is the set of elements of U that are not in A:
When B ⊆ A, the difference A \ B is the complement of B in A, sometimes written CAB. If you do not choose a universal set, the calculator uses U = A ∪ B, so Aᶜ = B \ A and Bᶜ = A \ B.
Venn diagram representation
A Venn diagram draws the universal set U as a rectangle and each set as a closed region inside it. Two overlapping circles split U into four regions, one for each basic operation:
- A only (blue): the elements of A \ B.
- The overlap (blended color): the elements of A ∩ B.
- B only (orange): the elements of B \ A.
- Outside both circles: the elements of U not in A ∪ B, that is (A ∪ B)ᶜ.
The union is everything inside the two circles. The diagram makes it clear that A \ B, A ∩ B and B \ A are pairwise disjoint and together form A ∪ B.
The inclusion–exclusion principle
Adding n(A) and n(B) counts every element of A ∩ B twice, so it must be subtracted once:
Example: in a class, 25 students like math, 20 like literature and 10 like both. Then 25 + 20 − 10 = 35 students like at least one of the two subjects.
Key laws of set algebra
- Commutative laws: A ∩ B = B ∩ A, A ∪ B = B ∪ A.
- Identity laws: A ∩ ∅ = ∅, A ∪ ∅ = A, A \ ∅ = A.
- If B ⊆ A, then A ∩ B = B and A ∪ B = A.
- De Morgan’s laws: (A ∪ B)ᶜ = Aᶜ ∩ Bᶜ and (A ∩ B)ᶜ = Aᶜ ∪ Bᶜ.
- Difference as an intersection: A \ B = A ∩ Bᶜ.
4 worked examples
Press Example in the calculator or type the same elements to check every step.
Example 1. Two overlapping sets
Let A = {−3, 0, 1.5, 2, 5, 8} and B = {0, 2, 4, 5, 9, 10}. Find A ∩ B, A ∪ B, A \ B and B \ A.
The common elements are 0, 2 and 5, so A ∩ B = {0, 2, 5}. Combining both sets and listing common elements once:
Check: n(A ∪ B) = 6 + 6 − 3 = 9. Removing the common elements from each set gives A \ B = {−3, 1.5, 8} and B \ A = {4, 9, 10}.
Example 2. Complement of a subset
Let A = {1, 2, 3, 4, 5, 6} and B = {2, 4, 6}. Show that B ⊆ A and find the complement of B in A.
Each element 2, 4, 6 of B is in A, so B ⊆ A. Hence A ∩ B = B, A ∪ B = A, B \ A = ∅, and:
In the Venn diagram, the region “B only” is empty.
Example 3. Complements in a universal set and De Morgan’s law
Let U = {0, 1, 2, …, 9}, let A be the even numbers and B the multiples of 3 in U: A = {0, 2, 4, 6, 8}, B = {0, 3, 6, 9}. Find Aᶜ and Bᶜ and verify De Morgan’s law.
We have A ∩ B = {0, 6} and A ∪ B = {0, 2, 3, 4, 6, 8, 9}. The complements in U are:
Their intersection is {1, 5, 7}, exactly the elements of U outside both circles: (A ∪ B)ᶜ = Aᶜ ∩ Bᶜ = {1, 5, 7}.
Example 4. Fractions, decimals and duplicates
Enter A = {−2.5, 1/2, 3, 0.5, 7} and B = {0.5, 3, 4, −1}.
Since 0.5 = 1/2, A really has four elements: A = {−2.5, 1/2, 3, 7}, n(A) = 4. Sorting B gives B = {−1, 0.5, 3, 4}, n(B) = 4. Therefore:
Check: n(A ∪ B) = 4 + 4 − 2 = 6. The differences are A \ B = {−2.5, 7} and B \ A = {−1, 4}.
Subsets of the real line: intervals and rays
Many sets of real numbers are written in interval notation. For real numbers a < b:
| Name | Notation | Set-builder form | Endpoints |
|---|---|---|---|
| Open interval | (a, b) | {x ∈ ℝ | a < x < b} | Neither a nor b |
| Closed interval | [a, b] | {x ∈ ℝ | a ≤ x ≤ b} | Both a and b |
| Half-open interval | [a, b) | {x ∈ ℝ | a ≤ x < b} | a only |
| Half-open interval | (a, b] | {x ∈ ℝ | a < x ≤ b} | b only |
| Open ray | (a, +∞) | {x ∈ ℝ | x > a} | a excluded |
| Closed ray | [a, +∞) | {x ∈ ℝ | x ≥ a} | a included |
| Open ray | (−∞, b) | {x ∈ ℝ | x < b} | b excluded |
| Closed ray | (−∞, b] | {x ∈ ℝ | x ≤ b} | b included |
| Real line | (−∞, +∞) = ℝ | All real x | None |
Infinity is not a real number, so the −∞ and +∞ ends always take a parenthesis: write [1, +∞), never [1, +∞]. Some textbooks (for example in Vietnam) separate the endpoints with a semicolon, [a; b], and some European books write ]a, b[ for an open interval; the calculator accepts all of these. Type infinity as +inf, -inf, +oo, -oo or use the +∞ and −∞ keys.
Plotting a set on the number line
- Draw a horizontal number line with the arrow pointing right (towards +∞) and mark the endpoints in increasing order.
- Use a square bracket [ or ] at an included endpoint (≤ or ≥) and a parenthesis ( or ) at an excluded endpoint (< or >). The bracket faces the part that is kept.
- Shade out regions not in the set with diagonal hatching ////. The unshaded part is the set.
For an operation on two sets, draw the lines for A and B one above the other, then a third line for the result. The intersection is the part left unshaded on both lines; the union is the part left unshaded on at least one line. (Some textbooks shade the part that belongs to the set instead; this tool follows the convention of shading out what is removed, so the answer stays clean.)
Operations on real intervals
- Intersection A ∩ B: the left endpoint is the larger left endpoint and the right endpoint is the smaller right endpoint. An endpoint is included when it belongs to both A and B. If the left end passes the right end, A ∩ B = ∅.
- Union A ∪ B: if A and B overlap or touch (like (−2, 4] and [4, 7]), merge them into one interval from the smallest left endpoint to the largest right endpoint; otherwise keep the union of two pieces. An endpoint is included when it belongs to A or to B.
- Difference A \ B: remove from A the part it shares with B, i.e. A \ B = A ∩ Bᶜ. At each cut point the bracket flips: an endpoint that belongs to B (square bracket) is removed and becomes a parenthesis; an endpoint not in B (parenthesis) is kept and becomes a square bracket.
- Complement Aᶜ = CℝA = ℝ \ A: take the parts of the line outside A and flip every bracket. For a < b:
Worked examples with intervals
Switch the calculator to Intervals & Number Line, enter the sets below and pick the matching operation to see the three number lines and the four-step solution.
Example 5. Intersection and union of two intervals
Let A = [−4, 1] and B = [0, 3). Find A ∩ B and A ∪ B.
In set-builder form, A = {x ∈ ℝ | −4 ≤ x ≤ 1} and B = {x ∈ ℝ | 0 ≤ x < 3}. The endpoints in order are −4 < 0 < 1 < 3. The intersection runs from max(−4, 0) = 0 to min(1, 3) = 1, and both 0 and 1 are in A and in B, so both brackets are square:
The intervals overlap, so the union is a single half-open interval; 3 is in neither set, so it keeps its parenthesis.
Example 6. Set difference and the bracket-flip rule
Let A = [−3, 5) and B = (1, 2]. Find A \ B and B \ A.
The complement of B is Bᶜ = (−∞, 1] ∪ (2, +∞). Removing B from A:
At 1, B is open, so 1 ∉ B: the point 1 stays and gets a square bracket. At 2, 2 ∈ B, so it is removed and gets a parenthesis. Every number in B is in A (B ⊆ A), so B \ A = ∅.
Example 7. Complements of rays and a disjoint union
Let A = [1, +∞) and B = (−∞, 0). Find Aᶜ, Bᶜ and A ∪ B.
Flipping the brackets gives Aᶜ = (−∞, 1) and Bᶜ = [0, +∞). There is a gap [0, 1) between B and A, so the union cannot be merged:
Practice problems
- Interval notation. Write A = {x ∈ ℝ | −2 < x ≤ 5} and B = {x ∈ ℝ | x ≥ 3} as intervals. Answer: A = (−2, 5], B = [3, +∞).
- Intersection and union. A = (−∞, 3), B = [−1, 6]. Answer: A ∩ B = [−1, 3), A ∪ B = (−∞, 6].
- Difference. A = [0, 8], B = (2, 5). Answer: A \ B = [0, 2] ∪ [5, 8], B \ A = ∅.
- Complement. A = (−3, 4]. Answer: Aᶜ = (−∞, −3] ∪ (4, +∞).
- Parameter for A ⊆ B. A = [m, m + 2], B = (1, 5). A ⊆ B when 1 < m and m + 2 < 5, that is 1 < m < 3.
- Parameter for A ∩ B ≠ ∅. A = (−∞, m), B = [2, +∞). A ∩ B ≠ ∅ exactly when m > 2.
Enter problems 2, 3 and 4 in the calculator to check the answers and see how each number line is shaded.
Common mistakes
- Listing a common element twice in A ∪ B. Every element of a set appears only once.
- Confusing A \ B with B \ A. A \ B starts from A and removes the elements that are also in B.
- Taking a complement without a universal set. Aᶜ only makes sense relative to a stated universal set U.
- Treating 0.5 and 1/2 as different elements. They are the same real number.
- Forgetting the empty set. If two sets share no element, the answer is A ∩ B = ∅, which still is a set.
- Closing a bracket at infinity, as in [1, +∞]. Infinity is not a number, so it always takes a parenthesis.
- Forgetting to flip brackets in a complement or difference. [a, b)ᶜ = (−∞, a) ∪ [b, +∞), not (−∞, a] ∪ (b, +∞).
- Merging intervals that only nearly touch. [1, 2) ∪ (2, 3] is not [1, 3], because 2 belongs to neither piece.
Frequently asked questions
What is the difference between union and intersection?
The intersection A ∩ B contains only the elements that are in both A and B. The union A ∪ B contains every element that is in A or in B, with common elements listed once. The intersection is always a subset of the union.
Is A \ B the same as B \ A?
Not in general. A \ B is the set of elements of A that are not in B, while B \ A is the set of elements of B that are not in A. They are equal only when A = B, in which case both are empty.
What is the difference between a relative complement and an absolute complement?
The relative complement of B in A is A \ B, the elements of A outside B. The absolute complement of A, written Aᶜ or A′, is U \ A, taken relative to a universal set U. Without a stated U, the calculator uses U = A ∪ B.
How do you find the number of elements in A ∪ B?
Use the inclusion–exclusion principle: n(A ∪ B) = n(A) + n(B) − n(A ∩ B). The common elements are counted twice when you add n(A) and n(B), so they are subtracted once. The calculator checks every union with this formula.
Can I enter fractions and decimals?
Yes. The calculator accepts integers, decimals such as -2.5 and fractions such as 1/2 or -3/4. Equal values such as 0.5 and 1/2 are treated as one element, and every set is sorted in increasing order.
What is the difference between open, closed and half-open intervals?
An open interval (a, b) excludes both endpoints, a closed interval [a, b] includes both, and a half-open interval [a, b) or (a, b] includes exactly one. On a number line a square bracket includes the endpoint, a parenthesis excludes it, and the infinite end of a ray always takes a parenthesis.
How do you find the complement of an interval?
The complement Aᶜ = ℝ \ A contains the real numbers not in A: take the parts of the line outside A and flip every bracket, for example [−4, 1]ᶜ = (−∞, −4) ∪ (1, +∞). The Intervals & Number Line mode computes complements and plots them on a number line.