实验了@skandhas的方法:
irb(main):001:0> arr=[1, 1, 1, 2, 2, 3]
=> [1, 1, 1, 2, 2, 3]
irb(main):002:0> arr.group_by{|v| v}
=> {1=>[1, 1, 1], 2=>[2, 2], 3=>[3]}
irb(main):003:0> arr.group_by{|v| v}.map{|k, v| [k, v.size] }
=> [[1, 3], [2, 2], [3, 1]]
irb(main):004:0> Hash[arr.group_by{|v| v}.map{|k, v| [k, v.size] }]
=> {1=>3, 2=>2, 3=>1}
Hash 可以这么初始化,很方便耶
Hash[ [ [key, value], ... ] ] → new_hash