Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

If a list contains duplicate elements, for example

 list = {a, 1, 5, 3, 5, x^2, x^2},

how can the duplicate elements be removed? The result would be

 uniqueElements = {a, 1, 5, 3, x^2}
share|improve this question
16  
Go to help function, type "delete duplicate elements", first two results. – David Feb 28 '12 at 17:00
@David it should be noted that searching for that on the online documentation gives different results depending on who's doing the searching. For instance, Union is towards the bottom, and DeleteDuplicates is on the second page for me. For Heike, both were on the first page. However, the built-in documentation center works fine. – rcollyer Mar 1 '12 at 15:33
2  
@rcollyer Google: "delete duplicate elements mathematica", first result. Second one also works. Bing: second result. – David Mar 1 '12 at 18:12

3 Answers

up vote 17 down vote accepted

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} *)
share|improve this answer

In version 7 or later, use the DeleteDuplicates function.

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.

For Mathematica 4 or earlier

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

For Mathematica 5

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]]]]
share|improve this answer
Wizard I like the Leonid's solution for the old versions of Mathematica. Not too relevant anymore, but still cool. – Pillsy Feb 28 '12 at 16:33
+1. Actually, I had an impression that Dispatch first appeared only in 4.1, but I may be mistaken. Also, IIRC, the first solution is a version of the solution first published by Carl Woll. – Leonid Shifrin Feb 28 '12 at 16:59
@LeonidShifrin The first two solutions are also found in the documentations of the respective versions. That is where I learned about them. The docs say Dispatch is from v2, but I start using Mma only at v4, and at that time I didn't even know about Dispatch, so I don't know if it changed. – Szabolcs Feb 28 '12 at 17:18
@Szabolcs Interesting. I knew that the Reap-Sow one is in the docs, but not about the one by Carl. You may be right about Dispatch. – Leonid Shifrin Feb 28 '12 at 17:38
{a, 1, 5, 3, 5, x^2, x^2} //. {a___, b_, c___, b_, d___} :> {a, b, c, d}, perhaps originally due to David Park. – TomD Feb 28 '12 at 17:52
show 1 more comment

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} *)
share|improve this answer
5  
Since DeleteDuplicates is faster than Union I would say this is if you do care about sorting, specifically that you want it. – Mr.Wizard Feb 28 '12 at 16:05
1  
@Mr.Wizard: Fair enough, but then I should mention that with Union you have to type less :) – István Zachar Feb 28 '12 at 17:16
5  
Haha, true, but if you don't feel like typing the best advice is to not use Mathematica... NotebookSetupLayoutInformationPacket, MultivariateHypergeometricDistribution, NormalizedSquaredEuclideanDistance or the ironic VerboseConvertToPostScriptPacket – Rojo Feb 28 '12 at 18:02
@Rojo reminds me of generic programming in c++. – rcollyer Feb 29 '12 at 17:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.