Skip to content

SetMinus ​

The elements of set a that are not also in set b.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaSet differenceMathWorldSetDifferenceWikidataQ845126FungrimSetMinusWolfram LanguageComplement!
SetMinus(a, b)the elements of a that are not also in b.
Typed signatures
TypePackageApplies
(set<any>, value*) -> setcompute-enginealways

Domain: Collections

Details
  • Not commutative: A∖BA \setminus B generally differs from B∖AB \setminus A.
  • Removing elements that aren't present in a leaves it unchanged.
  • Identity: A∖(A∖B)=A∩BA \setminus (A \setminus B) = A \cap B. See Intersection.
  • Subtracting a superset of a collapses the result to EmptySetEmptySet.
  • The result preserves encounter order rather than sorting into a canonical order.

Examples

Basic #
live ↗
In{1,2,3}∖{2}\lbrace1, 2, 3\rbrace∖\lbrace2\rbraceOut{1,3}\lbrace1, 3\rbrace
live ↗
In{1,2,3,4}∖{2,4}\lbrace1, 2, 3, 4\rbrace∖\lbrace2, 4\rbraceOut{1,3}\lbrace1, 3\rbrace

Removing elements that aren't present leaves the set unchanged

live ↗
In{1,2,3}∖{9}\lbrace1, 2, 3\rbrace∖\lbrace9\rbraceOut{1,2,3}\lbrace1, 2, 3\rbrace

Several sets can be removed at once

live ↗
In{a,b,c,d,f}∖{a,c}∖{d}\lbrace a, b, c, d, f\rbrace∖\lbrace a, c\rbrace∖\lbrace d\rbraceOut{b,f}\lbrace b, f\rbrace
live ↗
In{a,b,c,d,k}∖{a,c}∖{d}\lbrace a, b, c, d, k\rbrace∖\lbrace a, c\rbrace∖\lbrace d\rbraceOut{b,k}\lbrace b, k\rbrace
Scope #

Lists as well as sets, as Wolfram's ComplementComplement takes

live ↗
In[a,b,c,d,f]∖List⁡(a,c)∖[d]\bigl\lbrack a, b, c, d, f\bigr\rbrack∖\operatorname{List}(a, c)∖\bigl\lbrack d\bigr\rbrackOut{b,f}\lbrace b, f\rbrace

A Range as the universe to remove from

live ↗
In(1..10)∖{2,3}(1..10)∖\lbrace2, 3\rbraceOut{1,4,5,6,7,8,9,10}\lbrace1, 4, 5, 6, 7, 8, 9, 10\rbrace
Properties #

A∖(A∖B)=A∩BA \setminus (A \setminus B) = A \cap B. See Intersection

live ↗
In{1,2,3,4}∖({1,2,3,4}∖{2,4})={1,2,3,4}∩{2,4}\lbrace1, 2, 3, 4\rbrace∖(\lbrace1, 2, 3, 4\rbrace∖\lbrace2, 4\rbrace)=\lbrace1, 2, 3, 4\rbrace∩\lbrace2, 4\rbraceOutTrue\mathrm{True}

Removing the empty set changes nothing: A∖∅=AA \setminus \varnothing = A

live ↗
In{1,2,3}∖∅\lbrace1, 2, 3\rbrace∖∅Out{1,2,3}\lbrace1, 2, 3\rbrace

A∖A=∅A \setminus A = \varnothing

live ↗
In{1,2,3}∖{1,2,3}\lbrace1, 2, 3\rbrace∖\lbrace1, 2, 3\rbraceOut∅∅
live ↗
In{b,k,d,a,c}∖{b,c}\lbrace b, k, d, a, c\rbrace∖\lbrace b, c\rbraceOut{a,d,k}\lbrace a, d, k\rbrace
Possible issues #

SetMinusSetMinus is not commutative: B∖AB \setminus A can differ wildly from A∖BA \setminus B

live ↗
In{2,4}∖{1,2,3,4}\lbrace2, 4\rbrace∖\lbrace1, 2, 3, 4\rbraceOut∅∅

The result is a set, so its elements are in canonical order

live ↗
In{5,4,3,2,1}∖{2}\lbrace5, 4, 3, 2, 1\rbrace∖\lbrace2\rbraceOut{1,3,4,5}\lbrace1, 3, 4, 5\rbrace
About
Examples13
Checked againstWolfram Language 10 agree2 differ
CrosswalkWikipediaMathWorldWikidataFungrimWolfram Language

See also: Union, Intersection