jissy
jissy

Reputation: 463

How to remove duplicate entries from a hash based on a value

I have a list with duplicate entries . I need a list with unique entries based on unit_id.

hash_list = {
  "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"},
  "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}
}

Expected Output:

hash_list = {
  "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}
}

Upvotes: 1

Views: 2137

Answers (2)

BinaryMee
BinaryMee

Reputation: 2142

hash_list.to_a
         .uniq! { |_, v| v['unit_id'] }
         .to_h

But note that, the duplicates are removed only based on the key unit_id.
To do it based on multiple keys,

hash_list.to_a
         .uniq! { |_, v| v.values_at('unit_id','_destroy') }
         .to_h

Please have a look at Hash#values_at

Output

>> hash_list = { "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"} }
#=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}}
>> hash_list.to_a.uniq! { |_, v| v.values_at('unit_id','_destroy') }.to_h
#=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}}

Upvotes: 5

Cary Swoveland
Cary Swoveland

Reputation: 110675

Here's a way that does not convert the hash to an array, modify the array, then convert the modified array back to a hash:

require 'set'

s = Set.new
hash_list.select { |_,h| s.add?(h["unit_id"]) }
  #=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  #    "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}} 

Upvotes: 1

Related Questions