fpbcjw
Last Updated: February 25, 2016
·
87
· dpaluy
3129de6b479cc965513311f1ddcc268a

Enumerable Histogram

module Enumerable

  def to_histogram
    inject(Hash.new(0)) { |h, x| h[x] += 1; h}
  end

end
%w(a b c d a a b).to_histogram

will return:
{
"a" => 3,
"b" => 2,
"c" => 1,
"d" => 1
}

Say Thanks
Respond