Calculating counts for all array items
Ever wondered how often a specific item occurs in an Array?
>> [1, 2, 1, 3, 4, 2, 1, 1, 3, 5, 5].grep(3).size
=> 2
Now, if we take that a step further, we can calculate the counts for all elements:
>> list = [1, 2, 1, 3, 4, 2, 1, 1, 3, 5, 5]
=> [1, 2, 1, 3, 4, 2, 1, 1, 3, 5, 5]
>> list.uniq.inject({}) { |res, item| res.merge(item => list.grep(item).size) }
=> {5=>2, 1=>4, 2=>2, 3=>2, 4=>1}
