Skip to content

Unique ​

The elements of a collection, each kept once, in first-occurrence order.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageDeleteDuplicates✓
Unique(xs)xsxs with every repeat of an element dropped, keeping the first occurrence.
Unique(xs, test)as above, but two elements count as duplicates when testtest holds for them, not just when they're structurally equal.
Typed signatures
TypePackageApplies
((T) -> T where T: string) & ((collection<T>) -> list<T> where T: any)compute-enginealways
(collection<any>, function?) -> list<any>combinatoricsreplaces compute-enginealways

Domain: Collections

Examples

Basic #
live ↗
InUnique([1,2,2,3,1])\mathrm{Unique}(\bigl\lbrack1, 2, 2, 3, 1\bigr\rbrack)Out[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack
live ↗
InUnique([a,b,a,c,b])\mathrm{Unique}(\bigl\lbrack a, b, a, c, b\bigr\rbrack)Out[a,b,c]\bigl\lbrack a, b, c\bigr\rbrack

Keeps first-occurrence order, without sorting

live ↗
InUnique([1,7,8,4,3,4,1,9,9,2,1])\mathrm{Unique}(\bigl\lbrack1, 7, 8, 4, 3, 4, 1, 9, 9, 2, 1\bigr\rbrack)Out[1,7,8,4,3,9,2]\bigl\lbrack1, 7, 8, 4, 3, 9, 2\bigr\rbrack

Removes repeats, keeping first occurrences in order

live ↗
InUnique([1,7,8,4,3,4,1,9,9,2])\mathrm{Unique}(\bigl\lbrack1, 7, 8, 4, 3, 4, 1, 9, 9, 2\bigr\rbrack)Out[1,7,8,4,3,9,2]\bigl\lbrack1, 7, 8, 4, 3, 9, 2\bigr\rbrack
Scope #

Elements can themselves be lists

live ↗
InUnique([[1,2],[1,2],[3]])\mathrm{Unique}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3\bigr\rbrack\bigr\rbrack)Out[[1,2],[3]]\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3\bigr\rbrack\bigr\rbrack
live ↗
InUnique([])\mathrm{Unique}(\bigl\lbrack\bigr\rbrack)Out[]\bigl\lbrack\bigr\rbrack

A second argument says when two elements count as duplicates -- here, when they differ by less than 2

live ↗
InUnique([1,2,3,4,5,6],(_1⁡,_2⁡)↦∣_1⁡−_2⁡∣<2)\mathrm{Unique}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\vert\operatorname{\_1}-\operatorname{\_2}\vert\lt2)Out[1,3,5]\bigl\lbrack1, 3, 5\bigr\rbrack

With a custom comparison, an element is dropped when it relates to an earlier kept one

live ↗
InUnique([1,7,8,4,3,4,1,9,8,9,2],Greater)\mathrm{Unique}(\bigl\lbrack1, 7, 8, 4, 3, 4, 1, 9, 8, 9, 2\bigr\rbrack, \mathrm{Greater})Out[1,7,8,9,9]\bigl\lbrack1, 7, 8, 9, 9\bigr\rbrack
Applications #

Count the distinct elements

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

The residues mod 3 that occur, in order of first appearance

live ↗
InUnique((1..10) mod 3)\mathrm{Unique}((1..10)\bmod3)Out[1,2,0]\bigl\lbrack1, 2, 0\bigr\rbrack
Properties #

Idempotent

live ↗
InUnique(Unique([1,1,2]))\mathrm{Unique}(\mathrm{Unique}(\bigl\lbrack1, 1, 2\bigr\rbrack))Out[1,2]\bigl\lbrack1, 2\bigr\rbrack
Possible issues #

A test that is never true treats nothing as a duplicate

live ↗
InUnique([3,3,3],()↦False)\mathrm{Unique}(\bigl\lbrack3, 3, 3\bigr\rbrack, ()↦\mathrm{False})Out[3,3,3]\bigl\lbrack3, 3, 3\bigr\rbrack
About
Examples12
Checked againstWolfram Language 12 agree
CrosswalkWolfram Language