I have a hash in which I want to use the values as keys in a new Hash which contains a count of how many times that item appeared as a value in the original hash.
So I use:
hashA.keys.each do |i|
puts hashA[i]
end
Example output:
0
1
1
2
0
1
1
And I want the new Hash to be the following:
{ 0 => 2, 1 => 4, 2 => 1 }
counts = hashA.values.inject(Hash.new(0)) do |collection, value|
collection[value] +=1
collection
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With