📜  ruby 获取数组中相同元素的数量 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:45.950000             🧑  作者: Mango

代码示例2
names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = names.each_with_object(Hash.new(0)){ |string, hash| hash[string] += 1 }
puts counts # => { Jason: 2, Teresa: 1, Judah: 3, ... }