CCalcNest AI

Set Operations Calculator

Perform set union, intersection, and difference.

Enter values above — results appear instantly as you type.
AI Insight: Union, intersection, and difference are the foundation of database queries and search filters. The common error is confusing 'or' (union) with 'and' (intersection) — in plain English they blur, but in set logic they're opposites.
Notice: This calculator is provided for educational reference. Results depend entirely on the values you enter, and you should verify any figure used for academic, professional, or safety-critical purposes. See our full disclaimer.
Written with AI assistance and checked by automated validation · Last updated: August 2026 · How we build and check this · Methodology
Looking for a different calculator? Try our AI Finder — describe what you need in plain English. Try AI Finder →

Formula

Union, Intersection, Difference

Example

A={1,2,3,4} B={3,4,5,6} → A∪B={1,2,3,4,5,6}.

Embed this calculator on your site

Add this free calculator to your own website with one line of code. The embedded version is responsive, ad-free, and includes a small attribution link back to CalcNest AI.

<iframe src="https://calcnestai.com/embed/set-operations-calculator.html" width="100%" height="700" frameborder="0" style="border: 1px solid #e5e5e5; border-radius: 12px; max-width: 720px;" loading="lazy" title="Set Operations Calculator — Free Tool by CalcNest AI"></iframe>

Understanding the Set Operations Calculator

A set calculator computes union, intersection, and difference between two sets. Sets are unordered collections of distinct elements, and both of those properties matter: duplicates collapse and order carries no information.

How it actually works

Enter two comma-separated lists. The calculator removes duplicates, then computes the elements in either set, in both, and in the first but not the second. Sets 1,2,3,4 and 3,4,5,6 give a union of six elements and an intersection of two.

The core operations
OperationMeaning
A ∪ BIn either set
A ∩ BIn both sets
A − BIn A but not B
A △ BIn exactly one, the symmetric difference

The deeper context most people miss

The symmetric difference is the set-theoretic equivalent of exclusive or, containing elements in one set or the other but not both. That correspondence between set operations and logical connectives is exact and is what makes set theory and Boolean algebra two views of the same structure.

Why sets underpin so much of mathematics

Set theory became the standard foundation for mathematics in the twentieth century, with essentially every mathematical object definable as a set: numbers, functions, relations, and structures all reduce to sets under standard constructions. Natural numbers are built from the empty set, with zero as the empty set, one as the set containing it, and so on. An ordered pair is definable as a particular set, which then defines relations and functions. That reduction gives mathematics a common foundation and is why set theory appears at the base of formal treatments. The route there was not smooth. Cantor's development of set theory in the 1870s introduced infinite sets of different sizes, showing that the rationals are countable while the reals are not, which was resisted strongly at the time. Naive set theory then proved inconsistent: Russell's paradox considers the set of all sets that do not contain themselves and asks whether it contains itself, with either answer contradicting itself. The response was axiomatic set theory, with Zermelo-Fraenkel plus the axiom of choice being the standard system, which restricts set formation enough to avoid the paradoxes. Gödel's incompleteness theorems then showed that no such system can prove its own consistency, which is a genuine limit on foundations rather than a technicality.

A worked example: sets in databases and code

Union, intersection, and difference on 1,2,3,4 and 3,4,5,6 correspond directly to operations people use constantly without naming them. SQL implements them as UNION, INTERSECT, and EXCEPT, with UNION removing duplicates and UNION ALL retaining them, which is a distinction that causes real bugs since UNION ALL is faster and returns different results. A JOIN is fundamentally a set operation on rows, with inner joins corresponding to intersection on the join key and outer joins bringing in the difference. In programming, most languages provide set types with these operations, and choosing a set over a list changes the complexity of membership testing from linear to roughly constant, which is why converting a list to a set before repeated lookups is one of the more effective simple optimisations. Deduplication is set construction. Tag and permission systems are set operations, with checking whether a user has a required permission being an intersection test. In search, boolean queries map directly onto set operations over document sets, and inverted indexes are built to make those operations fast. Version control computes differences between file sets. The pattern across all of these is that recognising a problem as set-theoretic frequently identifies the right data structure immediately.

Deciding when a set is the right structure

Sets suit membership testing, deduplication, and combining collections where order and repetition carry no meaning. They are the wrong structure when order matters, which calls for a list or sequence, or when counts matter, which calls for a multiset or a frequency map. That last case is worth watching: counting occurrences and then treating the result as a set discards exactly the information you gathered. Sets require elements to be hashable or comparable depending on the implementation, which constrains what can go in them, and mutable elements are dangerous as set members since changing one after insertion breaks the internal structure. Performance characteristics vary: hash-based sets give roughly constant membership testing with no ordering, while tree-based sets give logarithmic testing with sorted iteration, and the choice depends on whether ordered traversal is needed. For very large sets, probabilistic structures including Bloom filters test membership with a small false positive rate in a fraction of the memory, which suits cases where an occasional false positive is acceptable and a false negative is not, such as checking whether a key might be in a cache before performing an expensive lookup. Set operations on sorted collections can be done by merging in linear time without hashing, which matters at scale.

Infinite sets and different sizes of infinity

Cantor's diagonal argument shows that the real numbers cannot be put into one-to-one correspondence with the natural numbers, so some infinities are strictly larger than others. The argument is short and worth knowing: suppose a list claiming to contain every real number between zero and one, then construct a number differing from the first in its first digit, the second in its second, and so on, producing a real number absent from the list and contradicting the assumption. The rationals, by contrast, are countable despite seeming denser, which is shown by arranging them in a grid and traversing diagonally. The algebraic numbers are countable, so almost all real numbers are transcendental even though proving any particular number transcendental is difficult. The continuum hypothesis asks whether any set has size strictly between the naturals and the reals, and Gödel and Cohen established that it can neither be proved nor disproved from the standard axioms, making it independent of them, which was a genuinely startling result about the limits of the axiomatic method. The power set of any set is strictly larger than the set itself, so there is no largest infinity but an unending hierarchy. None of this is required for practical work and all of it changed how mathematicians understand what mathematics is.

Variations: multisets, fuzzy sets, and relations

Multisets or bags allow repeated elements and track counts, which suits frequency analysis and inventory. Ordered collections including lists and tuples preserve sequence. Fuzzy sets assign degrees of membership between zero and one rather than binary membership, and are used in control systems and some classification problems. Rough sets handle imprecise boundaries. Power sets contain all subsets and grow exponentially, with a set of n elements having two to the n subsets, which is why enumerating subsets is infeasible beyond modest sizes. Cartesian products pair every element of one set with every element of another and underlie relations and database joins. Partitions divide a set into disjoint covering subsets and correspond to equivalence relations. Venn diagrams visualise operations for up to three sets clearly and become unwieldy beyond that, with Euler diagrams handling cases where not all intersections exist. In databases, set semantics distinguish from bag semantics, and SQL is technically bag-based with explicit DISTINCT required for set behaviour, which is a frequent source of surprise for those expecting mathematical set semantics.

Working with sets effectively

Use a set when membership testing, deduplication, or combining collections is the operation, and a list when order matters or a multiset when counts do. Convert a list to a set before repeated membership tests, which changes lookup from linear to roughly constant and is a substantial easy optimisation. Note that duplicates collapse silently, so constructing a set from data destroys count information that may have been the point. Distinguish UNION from UNION ALL in SQL, since the first deduplicates and the second does not, and SQL is bag-based by default requiring explicit DISTINCT for set semantics. Avoid mutable elements as set members, since changing one after insertion corrupts the internal structure. Choose hash-based sets for speed and tree-based ones when sorted iteration is needed. Consider a Bloom filter for very large membership tests where a small false positive rate is acceptable. Remember that the power set grows as two to the n, making subset enumeration infeasible beyond modest sizes. And recognise the correspondence between set operations and logical connectives, which frequently clarifies both.

What people get wrong

  • Building a set from data whose counts matter, when duplicates collapse silently and the frequency information that motivated the work is discarded.
  • Using UNION where UNION ALL was intended in SQL, or the reverse, since one deduplicates and the other does not and the performance difference is substantial.
  • Testing membership repeatedly against a list, when converting to a set changes lookup from linear to roughly constant and is a simple large improvement.
  • Mutating an element after inserting it into a hash-based set, which corrupts the structure since the element's hash no longer matches its stored position.

Where the math comes from

A ∪ B contains elements in either set, A ∩ B contains those in both, and A − B contains those in A but not B. Sets are unordered and contain distinct elements, so duplicates collapse on construction. The symmetric difference contains elements in exactly one set and corresponds to logical exclusive or, mirroring the correspondence between set operations and Boolean connectives.

Questions and answers

What is the difference between percent and percentage point?

Percent change is relative (going from 5% to 10% is a 100% increase). Percentage point change is absolute (the same shift is a 5 percentage point increase). News stories often confuse these.

How do I calculate a discount?

Discount amount = original x discount %. Final price = original x (1 - discount %). For 20% off $100: discount $20, final $80.

What is the formula for compound percentage?

Final = original x (1 + r1) x (1 + r2) x ... where each r is a percentage as decimal. A 10% raise then 10% cut: 1.10 x 0.90 = 0.99 = 99% of original.

How do I reverse a percentage?

If $80 is 80% of original: original = $80 / 0.80 = $100. To reverse 'X% off' to find original: original = final / (1 - X/100).

How do percentages work in tax?

Marginal tax rate applies to income within a bracket. Effective rate is total tax / total income. They diverge because of progressive brackets.

What happens to duplicate entries?

They collapse, since a set contains each element once. That's a defining property rather than a side effect, and it means constructing a set from data discards any count information, which matters if frequencies were what you needed.

Does order matter in a set?

No. Two sets with the same elements in different orders are the same set. If order carries meaning, a list or sequence is the right structure, and treating an ordered collection as a set discards that information.

What is the symmetric difference?

Elements in exactly one of the two sets, in one or the other but not both. It corresponds directly to logical exclusive or, which reflects the exact correspondence between set operations and Boolean connectives.

When should I use a set instead of a list?

For membership testing, deduplication, and combining collections where order and repetition carry no meaning. Converting a list to a set before repeated lookups changes the cost from linear to roughly constant, which is a substantial and simple optimisation.

Why does SQL UNION differ from UNION ALL?

UNION removes duplicates and UNION ALL retains them. SQL is bag-based rather than set-based by default, so DISTINCT is required for mathematical set semantics, and UNION ALL is faster because it skips the deduplication step.

What is Russell's paradox?

The question of whether the set of all sets that don't contain themselves contains itself, where either answer contradicts. It showed naive set theory was inconsistent and led to axiomatic systems that restrict set formation enough to avoid it.

Are some infinities bigger than others?

Yes. Cantor's diagonal argument shows the reals cannot be listed in correspondence with the naturals, so they form a strictly larger infinity, while the rationals are countable. The power set of any set is strictly larger, giving an unending hierarchy.

Related calculators

Proportion Solver · Percentage · Decimal to Fraction · Golden Ratio · Percentage Change