How to remove duplicates from hash

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"}

···

--
Posted via http://www.ruby-forum.com/.

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

···

On May 9, 7:30 am, Lucky Nl <lakshmi2...@gmail.com> 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"}

Thankq so much Thomas Sawyer
Its working to me

···

--
Posted via http://www.ruby-forum.com/.

but how to do this manually.
here we know manually there 2 duplicates
how to check and remove dynnamical values

···

--
Posted via http://www.ruby-forum.com/.

ok in mycode something going wrong due to that result not get properly .
your sugetion working .thankq

···

--
Posted via http://www.ruby-forum.com/.