From: "Jesús Gabriel y Galán" Date: 2012-07-09T16:14:47+09:00 Subject: Re: Find that number is repeated more and less times in an array On Sun, Jul 8, 2012 at 10:13 PM, Joao Silva 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.