1

Say I have this array:

a = [1,2,3,1,4,5]

I want to group each unique element by how many times it's been used in the array.

So far I tried this, but I'm pretty sure there's a more efficient, Rubyist way:

b = Hash.new
a.uniq.each { |x| b[x] = a.count[x] }
# { 1 => 2, 2 => 1, 3 => 1, 4 => 1, 5 => 1 }
anemaria20
  • 1,646
  • 2
  • 17
  • 36

0 Answers0