Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: rule based program for "Deleting repeated members of a list."

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81990] Re: rule based program for "Deleting repeated members of a list."
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 8 Oct 2007 02:06:22 -0400 (EDT)
  • References: <fe7j44$qga$1@smc.vnet.net>

mumat schrieb:
> Hi,
> 
> I have a list s={a,b,c,c,d,e,e,f,g,g,g,h,a,b};
> 
> I need to write a program so that it reads the list and ignors
> repeated elements; so it outputs the following:
> 
> RepeatRemover[s]={a,b,c,d,e,f,g,h,a,b};
> 
> I am looking for a rule-based program to do this!
> 
> Any help would be greatly appreciated.
> 
> C.S.
> 
> 

Hi numat,


if you have to do this with patterns,

In[2]:= s //. {x___, y_, y_, z___} :> {x, y, z}
Out[2]= {a, b, c, d, e, f, g, h, a, b}

will do the task.

If you've got longer lists, this could last a while:

In[3]:=
SeedRandom[2007];
s = Flatten[Table[Random[Integer, {1, 8}],
      {k, Random[Integer, {1, 5}]}, {1234}]];

In[5]:=
AbsoluteTiming[s1 = s //. {a___, x_, x_, b___} :> {a, x, b}; ][[1]]
Out[5]= 74.0625 Second

Compare this to

In[6]:= AbsoluteTiming[s2 = First /@ Split[s]; ][[1]]
Out[6]= 0. Second

or (to put at least a pattern into the solution):

In[7]:=
AbsoluteTiming[
   s3 = First /@ DeleteCases[Partition[s, 2, 1, {1, 1}, "foo"],
      {x_, x_}];
][[1]]
Out[7]= 0. Second

and the reasults are the same:

In[8]:= s1 === s2 === s3
Out[8]= True


Regards,
Peter


  • Prev by Date: Re: Minor bug in the Documentation Centre interface
  • Next by Date: Re: rule based program for "Deleting repeated members of a list."
  • Previous by thread: Re: Re: rule based program for "Deleting repeated members of a list."
  • Next by thread: Re: rule based program for "Deleting repeated members of a list."