Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

programming DeleteRepetitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23662] programming DeleteRepetitions
  • From: Preston Nichols <pnichols at wittenberg.edu>
  • Date: Sun, 28 May 2000 23:09:11 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Mathematica Wizards:

Below I give a function which removes duplicates from a list (as Union
does), but without sorting the result (as Union also does).  More
specifically, it extracts, in order, the first instance of each distinct
element of the list.

Is there any simpler way to do this?  It's a simple idea, but I seem to
need seven different list-manipulation functions, including 3 uses of Map!

DeleteRepetitions[X_] :=
  Take[X, #] & /@
      Sort[First /@
          (Position[X, #] & /@
              Union[X])] // Flatten

For example,

In[2] := DeleteRepetitions[{3,1,2,3,3,2,4,1}]

Out[2] = {3,1,2,4}

In[3] := DeleteRepetitions[{b,a,b,a,c,a}]

Out[3] = {b,a,c}

I don't need to use this function on lists longer that 20 or so elements,
so speed is not a critical concern.

Also, my version happens to work on expressions with heads other than List
(because Take, Position, Union, and Flatten all do so), but I don't really
need that feature.

How would you implement this function?

Preston Nichols
Mathematics and Computer Science
Wittenberg University


  • Prev by Date: Re: polynomial order
  • Next by Date: Re: Command to get a notebook's directory?
  • Previous by thread: Bend magnet radiation, absorption, reflectivity with MATHEMATICA
  • Next by thread: Re: programming DeleteRepetitions