From: Joao Silva Date: 2012-07-09T05:13:08+09:00 Subject: Re: Find that number is repeated more and less times in an array 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: With 0 repeats found 1, 3 With 1 repeats found 4, 5, 6 With 2 repeats found 2 -- Posted via http://www.ruby-forum.com/.