From: Intransition Date: 2010-05-09T20:51:37+09:00 Subject: Re: how to remove duplicates from hash On May 9, 7:30 am, Lucky Nl wrote: > Hi , >   i have the values in hash is like >  list = {"1" => "ab" ,"2" => "ab","3" => "hjk"} >  now i want to remove duplicate value "ab" > i want the result like > > list = {"1" => "ab" ,"3" => "hjk"} Tricky, b/c order becomes important here, which traditionally is not "Hash". But if you are using Ruby 1.9+ insertion order is preserved so you can do it. The simplest way is probably: list.invert.invert But in that case you get: {"2" => "ab" ,"3" => "hjk"} If you can't adjust for that, then you can do something like list.to_a.reverse.to_h.invert.invert Where #to_h comes from Ruby Facets. Nice but not very efficient. We can do it ourselves a bit faster: class Hash def reverse_invert h = {} reverse_each{ |k,v| h[v] = k } h.invert end end ~Trans