I have a hash called count
, defined as count = {4=>2, 5=>3, 6=>3, 7=>1}
.
I want to take the max
value, and then push the key that corresponds to that value into an array, so I do this:
array = []
array.push(count.max_by{|k,v| v}[0])
=>> [5]
However, 6
also has the value 3
, which is another maximum value. How do I push this value into the array so I get [5,6]
instead of just [5]
?
This is the way to choose the
max
values of the hash:Use the
select
method on the hash:Get the keys:
And finally assign to an array: