Delete duplicate elements from a list
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 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]]]]
You can use DeleteDuplicates
to remove the duplicate elements while preserving the original order:
DeleteDuplicates[{a, 1, 5, 3, 5, x^2, x^2}]
(* {a, 1, 5, 3, x^2} *)
If you don't care about the original order (or if you want it sorted), use Union
:
Union@{a, 1, 5, 3, 5, x^2, x^2}
(* {1, 3, 5, a, x^2} *)