On Sun, Jul 8, 2012 at 10:13 PM, Joao Silva <lists / ruby-forum.com> wrote:
> Brian Candler wrote in post #1067902:
>
>> a = [1, 2, 2, 3, 5, 4, 6, 2, 6, 5, 4]
>> counts = Hash.new(0)
>> a.each { |val| counts[val] += 1 }
>> groups = {}
>> counts.each do |val,count|
>>   groups[count] ||= []
>>   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

> With 0 repeats found 1, 3
> With 1 repeats found 4, 5, 6
> With 2 repeats found 2

I think you should read a little bit about classes Array, Hash and
module Enumerable, and try yourself things around in IRB until you
feel more comfortable about how to use many of the facilities they
provide.

Jesus.