In version 7 or later, use the DeleteDuplicates
function. (See also DeleteDuplicatesBy
, introduced in version 10, but be aware of performance considerations.)
For versions of Mathematica before 7, when DeleteDuplicates
was introduced, and for general interest, here are several ways of implementing the UnsortedUnion
(i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union
. Unlike Union
, these functions do not sort the list in the process of removing duplicates.
These methods may be obsolete for the specific function of DeleteDuplicates
but they demonstrate methods that continue to be useful in more general problems.
- Derivatives of the first method below using
Sequence[]
:
- DeleteDuplicates while retaining sublist structureDeleteDuplicates while retaining sublist structure
- Selecting minimal subsets
The
Sow
/Reap
method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.The
Tally
method can be generalized toGatherBy
.
###For Mathematica 4 or earlier [ref]
UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &
###For Mathematica 5 [ref]
UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]
###For Mathematica 6
UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]
###From Leonid Shifrin for Mathematica 3+
unsortedUnion[x_List] :=
Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]