From: thomas.macklin@... Date: 2007-08-20T21:45:01+09:00 Subject: Re: Detecting duplicates in an array, anything in the standard library ? On Aug 19, 5:16 pm, Robert Klemme wrote: > On 19.08.2007 23:15, Robert Klemme wrote: > > > > > On 19.08.2007 12:38, 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"] > > > Actually you are not deleting duplicates as far as I can see. > > Did I say it's too late? Man, I should've worn my glasses... > > > Here's another one > > > irb(main):012:0> a.inject(Hash.new(0)) {|h,x| > > h[x]+=1;h}.inject([]){|h,(k,v)|h<1;h} > > => ["c"] > > > You could even change that to need just one iteration through the > > original array but it's too late and I'm too lazy. :-) > > Cheers > > robert or... require 'set' new_ary = ary.to_set.to_a #set strips dups.