10 Oct 2015
Hash[*data.group_by{ |v| v }.flat_map{ |k, v| [k, v.size] }]
which calculates histogram of given data and returns it as a hash.
Example usage:
data = [0,0,0,1,0,2,3,4,1,2,3,5,6, 'a', 'a', 'b', :d] Hash[*data.group_by{ |v| v }.flat_map{ |k, v| [k, v.size] }] # => {0=>4, 1=>2, 2=>2, 3=>2, 4=>1, 5=>1, 6=>1, "a"=>2, "b"=>1, :d=>1}