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”}
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”}
On May 9, 7:30 am, Lucky Nl [email protected] 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 likelist = {“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 F.s. 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
but how to do this manually.
here we know manually there 2 duplicates
how to check and remove dynnamical values
Thankq so much Thomas S.
Its working to me
ok in mycode something going wrong due to that result not get properly .
your sugetion working .thankq
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs