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

MathGroup Archive 2010

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

Search the Archive

Re: All componens different

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112025] Re: All componens different
  • From: "Dr. Wolfgang Hintze" <weh at snafu.de>
  • Date: Wed, 25 Aug 2010 06:04:33 -0400 (EDT)
  • References: <i4t5qb$g2q$1@smc.vnet.net>

"Dr. Wolfgang Hintze" <weh at snafu.de> schrieb im Newsbeitrag 
news:i4t5qb$g2q$1 at smc.vnet.net...
> Given a list v of natural numbers I'm looking for a function d[v_] 
> that
> returns True if all components are different and False otherwise.
>
> One solution is
>
> d1[v_] := ! Or @@ Flatten[Table[v[[i]] == v[[k]], {i, 1, Length[v]},
> {k, i + 1, Length[v]}]]
>
> Can you find more efficient (and elegant) solutions?
>
> Regards,
> Wolfgang
>
>
Thank you all for the various solutions proposed.
Here's a brief list:

The by far most frequent solution was

    d2[v_]:=Length[Union[v]]==Length[v]

Some also misunderstood my question by proposing (all equal instead of 
all different)

    d4[v_]:=Length[Union[v]]==1

Other related proposals were

    d5[v_]:= Length[Split[Sort[v]]] == Length[v]

    d6a[v_]:= Length[v] == Length[DeleteDuplicates[v]]
    d6b[v_]:= v === DeleteDuplicates[v]

    d7[v_]:= SameQ[Length[Union[v]], Length[v]]

    d8[v_]:= LeafCount[v] - 1 == Length[v]

Ray's solution is probably the most "literary" one (because we are 
looking for complete unequality, but it is slower on my computer)

    d3[v_]:=Unequal@@v

Thanks again and regards,
Wolfgang













  • Prev by Date: Re: 2 quick questions
  • Next by Date: A question about NDSolve
  • Previous by thread: Re: All componens different
  • Next by thread: Verifying an n-point polygon