Jean Jean - 1 year ago 61
Ruby Question

return key from hash with more values

I have the following hash

{"june"=>["sarah"], "sarah"=>["tom", "simon"], "tom"=>["claudiu"]}

and I need a way to return the key with most values on his array. Any suggestion or help?

Answer Source

If the value can always responds to a size message

h.max_by { |key, value| value.size }
 => ["sarah", ["tom", "simon"]]