# Complement (set theory)

If A is the area colored red in this image...
... then the complement of A is everything else.

In set theory, the complement of a set A refers to elements not in A.

When all sets under consideration are considered to be subsets of a given set U, the absolute complement of A is the set of elements in U but not in A.

The relative complement of A with respect to a set B, also termed the difference of sets A and B, written BA, is the set of elements in B but not in A.

## Absolute complement

The absolute complement of A (left circle) in U: .

### Definition

If A is a set, then the absolute complement of A (or simply the complement of A) is the set of elements not in A. In other words, if U is the universe that contains all the elements under study, and there is no need to mention it because it is obvious and unique, then the absolute complement of A is the relative complement of A in U:[1]

.

Formally:

The absolute complement of A is usually denoted by . Other notations include , , , , and .[2]

### Examples

• Assume that the universe is the set of integers. If A is the set of odd numbers, then the complement of A is the set of even numbers. If B is the set of multiples of 3, then the complement of B is the set of numbers congruent to 1 or 2 modulo 3 (or, in simpler terms, the integers that are not multiples of 3).
• Assume that the universe is the standard 52-card deck. If the set A is the suit of spades, then the complement of A is the union of the suits of clubs, diamonds, and hearts. If the set B is the union of the suits of clubs and diamonds, then the complement of B is the union of the suits of hearts and spades.

### Properties

Let A and B be two sets in a universe U. The following identities capture important properties of absolute complements:

De Morgan's laws:[3]
Complement laws:[3]
• (this follows from the equivalence of a conditional with its contrapositive).
Involution or double complement law:
Relationships between relative and absolute complements:
Relationship with set difference:

The first two complement laws above show that if A is a non-empty, proper subset of U, then {A, A} is a partition of U.

## Relative complement

### Definition

If A and B are sets, then the relative complement of A in B,[3] also termed the set difference of B and A,[4] is the set of elements in B but not in A.

The relative complement of A (left circle) in B (right circle):

The relative complement of A in B is denoted BA according to the ISO 31-11 standard. It is sometimes written BA, but this notation is ambiguous, as in some contexts it can be interpreted as the set of all elements ba, where b is taken from B and a from A.

Formally:

### Properties

Let A, B, and C be three sets. The following identities capture notable properties of relative complements:

• .
• .
• ,
with the important special case demonstrating that intersection can be expressed using only the relative complement operation.
• .
• .
• .
• .
• .
• .

## LaTeX notation

In the LaTeX typesetting language, the command \setminus[5] is usually used for rendering a set difference symbol, which is similar to a backslash symbol. When rendered, the \setminus command looks identical to \backslash except that it has a little more space in front and behind the slash, akin to the LaTeX sequence \mathbin{\backslash}. A variant \smallsetminus is available in the amssymb package.

## Complements in various programming languages

Some programming languages allow for manipulation of sets as data structures, using these operators or functions to construct the difference of sets a and b:

.NET Framework
a.Except(b);
C++
set_difference(a.begin(), a.end(), b.begin(), b.end(), result.begin());
Clojure
(clojure.set/difference a b)[6]
Common Lisp
set-difference, nset-difference[7]
Elm
Set.diff a b[8]
F#
Set.difference a b[9]
a - b[10]
Falcon
diff = a - b[11]
difference a b
a \\ b[12]
Java
diff = a.clone();
diff.removeAll(b);[13]
Julia
setdiff[14]
Mathematica
Complement[15]
MATLAB
setdiff[16]
OCaml
Set.S.diff[17]
Octave
setdiff[18]
PARI/GP
setminus[19]
Pascal
SetDifference := a - b;
Perl 5
# for perl version >= 5.10
@a = grep {not $_ ~~ @b} @a; Perl 6$A$B$A (-) $B # texas version PHP array_diff($a, $b);[20] Prolog a(X),\+ b(X). Python diff = a.difference(b)[21] diff = a - b[21] R setdiff[22] Racket (set-subtract a b)[23] Ruby diff = a - b[24] Rust let diff = std::collections::HashSet::difference(&hashset_a, &hashset_b); [25] Scala a.diff(b)[26] a -- b[26] Smalltalk (Pharo) a difference: b SQL SELECT * FROM A EXCEPT SELECT * FROM B Unix shell comm -23 a b[27] grep -vFxf b a # less efficient, but works with small unsorted sets XQuery (and XPath starting from version 2.0)$a except $b[28] Z Shell diff=("${(@)a:|b}")[29]

## Notes

1. The set other than A is thus implicitly mentioned in an absolute complement, and explicitly mentioned in a relative complement.
2. Bourbaki 1970, p. E II.6.
3. Halmos 1960, p. 17.
4. Devlin 1979, p. 6.
5. The Comprehensive LaTeX Symbol List
6. clojure.set API reference
7. Common Lisp HyperSpec, Function set-difference, nset-difference. Accessed on September 8, 2009.
8. elm-lang/core/5.1.1/Set. Accessed on March 24, 2018.
9. Set.difference<'T> Function (F#). Accessed on July 12, 2015.
10. Set.( - )<'T> Method (F#). Accessed on July 12, 2015.
11. Array subtraction, data structures. Accessed on July 28, 2014.
12. Data.Set (Haskell) Archived 2009-05-28 at the Wayback Machine.
13. Set (Java 2 Platform SE 5.0). JavaTM 2 Platform Standard Edition 5.0 API Specification, updated in 2004. Accessed on February 13, 2008.
14. Archived 2014-10-15 at the Wayback Machine.. The Standard Library--Julia Language documentation. Accessed on September 24, 2014
15. Complement. Mathematica Documentation Center for version 6.0, updated in 2008. Accessed on March 7, 2008.
16. Setdiff. MATLAB Function Reference for version 7.6, updated in 2008. Accessed on May 19, 2008.
17. . GNU Octave Reference Manual
18. PARI/GP User's Manual Archived September 11, 2015, at the Wayback Machine.
19. PHP: array_diff, PHP Manual
20. . Python v2.7.3 documentation. Accessed on January 17, 2013.
21. . The Racket Reference. Accessed on May 19, 2015.
22. Class: Array Ruby Documentation
23. The Rust Standard Library Documentation. Accessed on March 7, 2017.
24. scala.collection.Set. Scala Standard Library 2.11.7, Accessed on July 12, 2015.
25. comm(1) Archived 2008-10-19 at the Wayback Machine., Unix Seventh Edition Manual, 1979.
26. "XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)". W3C. 13 April 2015.
27. Parameter Expansion, The Z Shell Manual.

## References

• Bourbaki, N. (1970). Théorie des ensembles (in French). Paris: Hermann. ISBN 978-3-540-34034-8.
• Devlin, Keith J. (1979). Fundamentals of contemporary set theory. Universitext. Springer. ISBN 0-387-90441-7. Zbl 0407.04003.
• Halmos, Paul R. (1960). Naive set theory. The University Series in Undergraduate Mathematics. van Nostrand Company. Zbl 0087.04403.
• Weisstein, Eric W. "Complement". MathWorld.
• Weisstein, Eric W. "Complement Set". MathWorld.