```On Mon, Jul 9, 2012 at 9:14 AM, Jes=FAs Gabriel y Gal=E1n
<jgabrielygalan / gmail.com> wrote:
> On Sun, Jul 8, 2012 at 10:13 PM, Joao Silva <lists / ruby-forum.com> wrote:
>> Brian Candler wrote in post #1067902:
>>
>>> a =3D [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
>>> counts =3D Hash.new(0)
>>> a.each { |val| counts[val] +=3D 1 }
>>> groups =3D {}
>>> counts.each do |val,count|
>>>   groups[count] ||=3D []
>>>   groups[count] << val
>>> end
>>> groups.each do |count,vals|
>>>   puts "With #{count-1} repeats found #{vals.sort.join(", ")}"
>>> end
>>>
>>
>> One last thing, how do I print the results that I stay in order, because
>> the above code prints:
>>
>> With 0 repeats found 1, 3
>> With 2 repeats found 2
>> With 1 repeats found 4, 5, 6
>>
>> and I seek it to print in order:
>
> groups.sort.each do |count,vals|
>   puts "With #{count-1} repeats found #{vals.sort.join(", ")}"
> end

Alternative solution

irb(main):001:0> a =3D [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
=3D> [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
irb(main):002:0> max=3D0; counts =3D a.inject(Hash.new(0)) {|c, x|
cc=3D(c[x]+=3D1); max=3Dcc if cc>max; c}
=3D> {1=3D>1, 2=3D>3, 3=3D>1, 5=3D>2, 4=3D>2, 6=3D>2}
irb(main):003:0> 1.upto(max){|r| printf "With %d repeats: %p\n", r,
counts.select {|k,v| v=3D=3Dr}.map{|k,v|k} }
With 1 repeats: [1, 3]
With 2 repeats: [5, 4, 6]
With 3 repeats: [2]
=3D> 1

(Explanation omitted for pedagogical reasons.)

Have fun!

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

```