On May 9, 7:30=A0am, Lucky Nl <lakshmi2... / gmail.com> wrote:
> Hi ,
> =A0 i have the values in hash is like
> =A0list =3D {"1" =3D> "ab" ,"2" =3D> "ab","3" =3D> "hjk"}
> =A0now i want to remove duplicate value "ab"
> i want the result like
>
> list =3D {"1" =3D> "ab" ,"3" =3D> "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" =3D> "ab" ,"3" =3D> "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 =3D {}
      reverse_each{ |k,v| h[v] =3D k }
      h.invert
    end
  end

~Trans