给定一个数组,我想创建一个散列,其键是数组的唯一元素,其值是相应键在数组中出现的次数。
例如,如果数组是
["americans", "oath", "oath", "generation", "americans"]
关联的哈希值是
{ "americans"=>2, "oath"=>2, "generation"=>1 }
Enumerable#tally
方法:
[
"americans", "oath", "oath", "generation", "americans", "crisis", "crisis", "generation"
].tally
#=> { "americans" => 2, "oath" => 2, "generation" => 2, "crisis" =>2 }