       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:= s //. {x___, y_, y_, z___} :> {x, y, z}
Out= {a, b, c, d, e, f, g, h, a, b}

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

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

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

Compare this to

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

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

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

and the reasults are the same:

In:= s1 === s2 === s3
Out= 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."