Skip to content

Union ​

The union of the argument collections, sorted and de-duplicated; lists give a list, sets a set.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaUnion (set theory)MathWorldUnionWikidataQ185359Britannicatopic/union-set-theoryFungrimUnionWolfram LanguageUnion!
Union(a, b, …)the sorted, de-duplicated union of the lists, as a ListList (of sets, as a SetSet).
Typed signatures
TypePackageApplies
(any+) -> collectioncombinatoricsreplaces compute-enginealways
(any+) -> setcompute-enginealways

Domain: Collections

Details
  • Duplicates within and across all the argument collections are removed. Lists in give a list in Wolfram's canonical order (shorter sublists first); sets in give a set. With no arguments the result is EmptySetEmptySet (Wolfram's Union[] is the empty list).
  • Commutative and idempotent: A∪A=AA \cup A = A, and argument order doesn't affect the result.
  • Inclusion-exclusion: ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|. See Intersection and Length.
  • Sorted, matching Wolfram's Union — compute-engine's own de-duplication preserves first-seen order instead.

Examples

Basic #
live ↗
In[1,2,3]∪List⁡(3,4)\bigl\lbrack1, 2, 3\bigr\rbrack∪\operatorname{List}(3, 4)Out[1,2,3,4]\bigl\lbrack1, 2, 3, 4\bigr\rbrack
live ↗
InList⁡(1,2)∪List⁡(2,3)∪List⁡(3,4)\operatorname{List}(1, 2)∪\operatorname{List}(2, 3)∪\operatorname{List}(3, 4)Out[1,2,3,4]\bigl\lbrack1, 2, 3, 4\bigr\rbrack
live ↗
In[a,b,c]∪[b,c,d]\bigl\lbrack a, b, c\bigr\rbrack∪\bigl\lbrack b, c, d\bigr\rbrackOut[a,b,c,d]\bigl\lbrack a, b, c, d\bigr\rbrack
live ↗
In[a,b,a,c]∪[d,a,k,b]∪List⁡(c,a)\bigl\lbrack a, b, a, c\bigr\rbrack∪\bigl\lbrack d, a, k, b\bigr\rbrack∪\operatorname{List}(c, a)Out[a,b,c,d,k]\bigl\lbrack a, b, c, d, k\bigr\rbrack
Scope #

A single list is de-duplicated

live ↗
InUnion([1,2,1,3,6,2,2])\mathrm{Union}(\bigl\lbrack1, 2, 1, 3, 6, 2, 2\bigr\rbrack)Out[1,2,3,6]\bigl\lbrack1, 2, 3, 6\bigr\rbrack

Symbolic elements, merged across three lists

live ↗
In[a,b,a,c]∪[d,a,f,b]∪List⁡(c,a)\bigl\lbrack a, b, a, c\bigr\rbrack∪\bigl\lbrack d, a, f, b\bigr\rbrack∪\operatorname{List}(c, a)Out[a,b,c,d,f]\bigl\lbrack a, b, c, d, f\bigr\rbrack

Elements can themselves be lists; {1,2}\{1, 2\} and {2,1}\{2, 1\} stay distinct

live ↗
InList⁡([1,2],[1,2,3])∪List⁡([2,1],[1,2])∪List⁡([3,2,1],[1,2,3])\operatorname{List}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack1, 2, 3\bigr\rbrack)∪\operatorname{List}(\bigl\lbrack2, 1\bigr\rbrack, \bigl\lbrack1, 2\bigr\rbrack)∪\operatorname{List}(\bigl\lbrack3, 2, 1\bigr\rbrack, \bigl\lbrack1, 2, 3\bigr\rbrack)Out[[1,2],[2,1],[1,2,3],[3,2,1]]\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack2, 1\bigr\rbrack, \bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack3, 2, 1\bigr\rbrack\bigr\rbrack

With no arguments, the union is empty

live ↗
InOut∅∅

Sets in, a set out

live ↗
In{3,1}∪{2,3}\lbrace3, 1\rbrace∪\lbrace2, 3\rbraceOut{1,2,3}\lbrace1, 2, 3\rbrace

Lists in, a sorted de-duplicated list out, as in Wolfram

live ↗
In[3,1,3]∪[2]\bigl\lbrack3, 1, 3\bigr\rbrack∪\bigl\lbrack2\bigr\rbrackOut[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack
Applications #

Combining the divisors of 10 and 15 into one set

live ↗
InDivisors(10)∪Divisors(15)\mathrm{Divisors}(10)∪\mathrm{Divisors}(15)Out[1,2,3,5,10,15]\bigl\lbrack1, 2, 3, 5, 10, 15\bigr\rbrack

Combining the divisors of 10, 12 and 20 into one sorted set

live ↗
InDivisors(10)∪Divisors(12)∪Divisors(20)\mathrm{Divisors}(10)∪\mathrm{Divisors}(12)∪\mathrm{Divisors}(20)Out[1,2,3,4,5,6,10,12,20]\bigl\lbrack1, 2, 3, 4, 5, 6, 10, 12, 20\bigr\rbrack

Counting the distinct values in a list. See Length

live ↗
InLength(Union([1,2,1,3,6,2,2]))\mathrm{Length}(\mathrm{Union}(\bigl\lbrack1, 2, 1, 3, 6, 2, 2\bigr\rbrack))Out44

Seven of the eight possible 3-bit windows occur in the binary digits of 10!10!; 001001 never does

live ↗
InUnion(Partition(IntegerDigits(10!,2),3,1))\mathrm{Union}(\mathrm{Partition}(\mathrm{IntegerDigits}(10!, 2), 3, 1))Out[[0,0,0],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]\bigl\lbrack\bigl\lbrack0, 0, 0\bigr\rbrack, \bigl\lbrack0, 1, 0\bigr\rbrack, \bigl\lbrack0, 1, 1\bigr\rbrack, \bigl\lbrack1, 0, 0\bigr\rbrack, \bigl\lbrack1, 0, 1\bigr\rbrack, \bigl\lbrack1, 1, 0\bigr\rbrack, \bigl\lbrack1, 1, 1\bigr\rbrack\bigr\rbrack

Powers of 55 mod 1111 cycle through just five residues, the quadratic residues

live ↗
InUnion(NestList(_1⁡↦5_1⁡ mod 11,1,100))\mathrm{Union}(\mathrm{NestList}(\operatorname{\_1}↦5\operatorname{\_1}\bmod11, 1, 100))Out[1,3,4,5,9]\bigl\lbrack1, 3, 4, 5, 9\bigr\rbrack
Properties #

UnionUnion is commutative: argument order doesn't matter

live ↗
In[1,2,3]∪[2,3,4]=[2,3,4]∪[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack∪\bigl\lbrack2, 3, 4\bigr\rbrack=\bigl\lbrack2, 3, 4\bigr\rbrack∪\bigl\lbrack1, 2, 3\bigr\rbrackOutTrue\mathrm{True}

Inclusion-exclusion: ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|. See Length and Intersection

live ↗
InLength([1,2,3]∪[2,3,4])=(Length([1,2,3])+Length([2,3,4]))−Length([1,2,3]∩[2,3,4])\mathrm{Length}(\bigl\lbrack1, 2, 3\bigr\rbrack∪\bigl\lbrack2, 3, 4\bigr\rbrack)=(\mathrm{Length}(\bigl\lbrack1, 2, 3\bigr\rbrack)+\mathrm{Length}(\bigl\lbrack2, 3, 4\bigr\rbrack))-\mathrm{Length}(\bigl\lbrack1, 2, 3\bigr\rbrack∩\bigl\lbrack2, 3, 4\bigr\rbrack)OutTrue\mathrm{True}

The empty set is the identity for UnionUnion: A∪∅=AA \cup \varnothing = A

live ↗
In{1,2}∪∅\lbrace1, 2\rbrace∪∅Out{1,2}\lbrace1, 2\rbrace
About
Examples18
Checked againstWolfram Language 16 agree1 differ
CrosswalkWikipediaMathWorldWikidataBritannicaFungrimWolfram Language

See also: Intersection, SetMinus