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 >