Given an array I would like to create a hash whose keys are the unique elements of the array and whose values are the numbers of times the corresponding key appears in the array.
For example, if the array were
["americans", "oath", "oath", "generation", "americans"]
the associated hash would be
{ "americans"=>2, "oath"=>2, "generation"=>1 }
There is the
Enumerable#tally
method that does exactly that: