Mathematica 9 is now available
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

Re: programming DeleteRepetitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23683] Re: programming DeleteRepetitions
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 29 May 2000 12:24:11 -0400 (EDT)
  • References: <8gsout$eip@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Preston,
Here is a neat solution due to Carl Woll;


DeleteRepetitions[X_] :=
  Block[{t}, t[n_] := (t[n] = Sequence[]; n); t /@ X]


DeleteRepetitions[{3, 1, 2, 3, 3, 2, 4, 1}]

        {3, 1, 2, 4}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Preston Nichols" <pnichols at wittenberg.edu> wrote in message
news:8gsout$eip at smc.vnet.net...
> 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: JLink
  • Next by Date: RE: More Epilogs
  • Previous by thread: programming DeleteRepetitions
  • Next by thread: Re: programming DeleteRepetitions