Jean Jean - 1 month ago 8
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

If the value can always responds to a size message

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