如何找到最大值哈希的键? [英] How to find the key of the largest value hash?
本文介绍了如何找到最大值哈希的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下哈希值 {"CA"=>2, "MI"=>1, "NY"=>1}
如何使用 ruby 返回最大键值对?我希望它返回CA"
How can I return the maximum key value pair using ruby? I would like it to return "CA"
推荐答案
这将根据哈希元素的值返回最大哈希键值对:
This will return max hash key-value pair depending on the value of hash elements:
def largest_hash_key(hash)
hash.max_by{|k,v| v}
end
这篇关于如何找到最大值哈希的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文