Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Deleting duplicates from a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76027] Re: [mg75940] Deleting duplicates from a list
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Tue, 15 May 2007 04:47:27 -0400 (EDT)
  • References: <22511833.1179130674370.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

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