Union and Intersection Calculator
Calculate union intersection instantly with our math tool. Shows detailed work, formulas used, and multiple solution methods.
Reviewed for accuracy by Manoj Kumar, Mathematics Educator
Union and Intersection Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser โ no data is sent to any server.
Formula: |A U B| = |A| + |B| - |A n B|
Worked example โ Union: 7 elements, Intersection: 3 elements, Jaccard Similarity: 3/7 = 0.4286
Formula
|A U B| = |A| + |B| - |A n B|
The Inclusion-Exclusion Principle: the size of the union equals the sum of individual set sizes minus the size of the intersection. This avoids double-counting elements that appear in both sets.
Worked Examples
Example 1: Numeric Set Operations
Problem:Find the union, intersection, and differences of A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7}.
Solution:A U B = {1, 2, 3, 4, 5, 6, 7} (all unique elements) A n B = {3, 4, 5} (common elements) A - B = {1, 2} (in A but not B) B - A = {6, 7} (in B but not A) Symmetric Difference = {1, 2, 6, 7} Verify: |A U B| = 7 = |A| + |B| - |A n B| = 5 + 5 - 3 = 7
Result:Union: 7 elements, Intersection: 3 elements, Jaccard Similarity: 3/7 = 0.4286
Example 2: Disjoint Sets Example
Problem:Find operations for A = {a, b, c} and B = {x, y, z}.
Solution:A U B = {a, b, c, x, y, z} A n B = {} (empty set - no common elements) A - B = {a, b, c} = A B - A = {x, y, z} = B Symmetric Difference = {a, b, c, x, y, z} = A U B |A U B| = 6 = 3 + 3 - 0
Result:Sets are disjoint. Union has 6 elements. Intersection is empty. Jaccard Similarity: 0.
Frequently Asked Questions
What is the union of two sets and how is it calculated?
The union of two sets A and B, written as A U B, is the set containing all elements that are in A, in B, or in both. No element is repeated in the result even if it appears in both sets. For example, if A = {1, 2, 3} and B = {2, 3, 4}, then A U B = {1, 2, 3, 4}. The union operation combines both sets while eliminating duplicates. This concept is fundamental in database queries (SQL UNION), probability theory (where P(A or B) relates to set union), and data analysis when merging datasets from different sources.
What is the intersection of two sets and what does it represent?
The intersection of two sets A and B, written as A n B or A and B, contains only the elements that appear in BOTH sets simultaneously. For example, if A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, then A n B = {3, 4}. The intersection identifies the common elements shared between sets. In database terms, this corresponds to an INNER JOIN. In probability, the intersection relates to the probability of both events occurring. In everyday language, the intersection answers questions like which customers appear in both the online and in-store purchase lists.
What is the Inclusion-Exclusion Principle for sets?
The Inclusion-Exclusion Principle states that the cardinality (size) of the union equals the sum of individual cardinalities minus the intersection cardinality. In formula form: |A U B| = |A| + |B| - |A n B|. We subtract the intersection because those elements were counted twice, once in each set. For example, if |A| = 10, |B| = 8, and |A n B| = 3, then |A U B| = 10 + 8 - 3 = 15. This principle extends to three or more sets with alternating addition and subtraction. It is widely used in combinatorics and probability to count elements without double-counting overlapping regions.
What does it mean for two sets to be disjoint?
Two sets are disjoint (also called mutually exclusive) when they have no elements in common, meaning their intersection is the empty set. For example, the set of even numbers and the set of odd numbers are disjoint. If A = {1, 2, 3} and B = {4, 5, 6}, then A n B = {} and the sets are disjoint. When sets are disjoint, the union size simply equals the sum of their individual sizes since there is no overlap to subtract. In probability theory, disjoint events cannot occur simultaneously, which simplifies the addition rule to P(A or B) = P(A) + P(B). Testing for disjointness is a common operation in data validation.
What is the Jaccard Similarity Index and how is it useful?
The Jaccard Similarity Index (also called Jaccard coefficient) measures how similar two sets are by dividing the size of their intersection by the size of their union: J(A,B) = |A n B| / |A U B|. The result ranges from 0 (completely different, no overlap) to 1 (identical sets). For example, if A = {1,2,3,4} and B = {3,4,5,6}, the intersection has 2 elements and the union has 6, so J = 2/6 = 0.333. This metric is widely used in machine learning for comparing predicted vs actual classifications, in search engines for duplicate detection, in ecology for comparing species populations, and in recommendation systems for finding similar users or items.
What is a subset and how does it relate to union and intersection?
A set A is a subset of B (written A is a subset of B) if every element of A is also in B. When A is a subset of B, interesting simplifications occur: A n B = A (the intersection equals the smaller set) and A U B = B (the union equals the larger set). Every set is a subset of itself, and the empty set is a subset of every set. If A is a subset of B AND B is a subset of A, then A and B are equal sets. Testing subset relationships is essential in access control systems, data validation, and mathematical proofs. Understanding subset relationships helps predict the results of union and intersection operations.
How are set operations used in database queries and programming?
Set operations directly correspond to fundamental database operations. SQL UNION combines result sets (set union), INTERSECT finds common rows (set intersection), and EXCEPT or MINUS finds rows in one table but not another (set difference). In programming, Python has built-in set operations using operators: | for union, & for intersection, - for difference, and ^ for symmetric difference. JavaScript uses Set objects with methods like forEach and has to implement these operations. Understanding set theory helps write efficient queries and algorithms for data deduplication, access control lists, filtering, and finding matching records across multiple data sources.
What is the complement of a set and how is it defined?
The complement of a set A (written as A-prime or A-complement) contains all elements in the universal set that are NOT in A. The universal set U must be defined for the complement to be meaningful. For example, if U = {1,2,3,4,5} and A = {1,3,5}, then the complement of A = {2,4}. Key properties include: the complement of the complement of A equals A, the union of A and its complement equals U, and the intersection of A and its complement is the empty set. De Morgan's Laws connect complements with union and intersection: the complement of (A U B) equals (complement of A) n (complement of B).
How do Venn diagrams represent union and intersection visually?
Venn diagrams use overlapping circles to represent sets and their relationships visually. Each circle represents a set, and the overlapping region represents the intersection (elements in both sets). The union is represented by the entire area covered by both circles combined. The non-overlapping parts of each circle represent the set differences (A - B and B - A). The symmetric difference is the total shaded area excluding the overlap. Venn diagrams make abstract set operations intuitive and are particularly helpful for teaching probability, logic, and data analysis concepts. For three sets, the diagram shows seven distinct regions, each representing a unique combination of membership.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator ยท Editorial policy
Related Calculators
๐งฎSet Operations Calculator
Perform union, intersection, difference, and complement operations on sets.
๐งฎIntersection of Two Lines Calculator
Calculate intersection of two lines with inputs, formulas, and instant results.
๐งฎLine of Intersection of Two Planes Calculator
Calculate line of intersection of two planes with inputs, formulas, and instant results.
๐งฎAnnulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
๐งฎArea Calculator
Calculate area with inputs, formulas, and instant results.
๐งฎArea of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
๐งฎArea of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
๐งฎCenter of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.