Ari Brown
8/19/2007 1:01:00 PM
On Aug 19, 2007, at 6:39 AM, Thibaut Barrère wrote:
> Hi!
>
> Just wondering if there is something simple already built in the std
> library to remove duplicates from an array (or an enumerable). I've
> seen and used various approaches, like:
>
> module Enumerable
> def dups
> inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys
> end
> end
>
> which will give:
>
>> %w(a b c c).dups
> => ["c"]
>
> Anything more elegant ?
Couldn't you also just do a union with itself?
a = %w(a b c b a)
b = a & a #=> ["a", "b", "c"]
Score one for me :-))
~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.