MathGroup Archive 2007

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

Search the Archive

Re: Deleting duplicates from a list


If you want a sorted result, use Union:

Union[{8, 8, 11, 11, 19, 7, 7, 7, 8, 8}]

{7, 8, 11, 19}

If not, you can use

unsortedUnion[k_List] :=
  Module[{f}, f[x_] := (f[x] = Sequence[]; x); f /@ k]
unsortedUnion@{12, 8, 8, 11, 11, 19}

{12, 8, 11, 19}

If you know the original list consists of a series of "runs", you can us=
e

First /@ Split[{8, 8, 11, 11, 19}]

{8, 11, 19}

That works if the original list doesn't have multiple runs of the same  =

value.

The following result still has duplicates, but it could, in some  =

circumstance, be the thing you need:

First /@ Split[{8, 8, 11, 11, 19, 7, 7, 7, 8, 8}]

{8, 11, 19, 7, 8}

Bobby

On Mon, 14 May 2007 02:25:02 -0500, Raul Martinez <raulmart at mac.com> wro=
te:

> Hi all,
>
> I have a list, e.g., {8, 8, 11, 11, 19}. I want to eliminate all
> duplicates so the list becomes {8, 11, 19}. Can anyone suggest a
> command that will do this?
>
> TIA,
>
> Raul Martinez
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Re: Why Sort[{"AX", "!D", "EX"}] -> {"AX", "!D", "EX"}
  • Next by Date: Re: Deleting duplicates from a list
  • Previous by thread: Re: Deleting duplicates from a list
  • Next by thread: Re: Deleting duplicates from a list