T
Thibaut Barrère
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:
Anything more elegant ?
cheers
Thibaut
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:
=> ["c"]%w(a b c c).dups
Anything more elegant ?
cheers
Thibaut