Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Re: Unsorted Union

  • To: mathgroup at
  • Subject: [mg61625] Re: [mg61564] Re: Unsorted Union
  • From: ggroup at
  • Date: Mon, 24 Oct 2005 01:44:09 -0400 (EDT)
  • References: <dj4p6t$gpt$><dj721s$cpb$> <>
  • Reply-to: ggroup at
  • Sender: owner-wri-mathgroup at

On Saturday, October 22, 2005 at 03:24 GMT -0400, Bill White wrote:

> Other interesting things can be done with this memoizing scheme.  For
> example, it can be used to collect the duplicate elements in a list:

Very nice example.  However, I think you need a slight tweak to the
definition if you want to remove *all* occurrences of duplicated items.
This definition removes only the first occurrence of any given entry
from a list.  Therefore, if your list has some element listed 3 or
more times, the output list would still have multiple occurrences of
that element.

> In[11]:=
> DuplicateElements[list_List]:=
>   Module[{f,dups={}},
>     f[n_]:=(f[n]:=AppendTo[dups,n]);
>     f/@list;
>     dups];

One way to avoid duplicated entries in the resulting list could be:

DuplicateElements[list_List] :=
    Module[{f, dups = {}},
      f[n_]:=(f[n]:=(AppendTo[dups, n]; f[n] = Sequence[]));
      f /@ list;

  • Prev by Date: Zero-based indexing option?
  • Next by Date: Re: Re: regress versus fit - force throughzero/forceconstant term to zero
  • Previous by thread: Re: Unsorted Union
  • Next by thread: Re: Re: Unsorted Union