Topic: eliminate duplicate key based on value

HI friends ,

i have hash as below

list = {"18" => 1,"15" => 2,"15" => 3,"17" => 4,"18" => 5}
above hash there 2 duplicatekeys with values 2,3.I want to keep only one key=> value based on value i prefered
here  i want to get result as below if i select value  3
{"18" => 1,"15" => 3,"17" => 4,"18" => 5}

If i select value 2 i want result as   {"18" => 1,"15" => 2,"17" => 4,"18" => 5}
Can anybody help me

Re: eliminate duplicate key based on value

You can't get a hash with duplicate keys. If you create literal as you mentioned in your post, the last key-value pair override the first one. You can check it:

list.each { |k, v| puts "#{k}: #{v}" }

Re: eliminate duplicate key based on value

If i created hash likethat how to get result which i want