The first that came to my mind.
[4,5,6,4,5,6,6,7].inject(Hash.new(0)) {|res, x| res[x] += 1; res }
Is it good enough for you?
On Jan 16, 2012, at 5:51 PM, Ralph Shnelvar wrote:
> a = [4,5,6,4,5,6,6,7]
>
> result = Hash.new(0)
> a.each { |x| result[x] += 1 }
>
> p result
>
>
>
> The result I am getting
> {4=>2, 5=>2, 6=>3, 7=>1}
> is what I want.
>
> Is there a better way; perhaps using uniq?
>
>