From: Hans Mackowiak Date: 2012-07-08T15:43:39+09:00 Subject: Re: Find that number is repeated more and less times in an array Hash[a.group_by{|x|a.count(x)-1}.map{|k,v|[k,v.uniq]}] =>{0=>[1, 3], 2=>[2], 1=>[5, 4, 6]} or a.group_by{|x|a.count(x)-1}.each_with_object({}){|(k,v),h|h[k]=v.uniq} =>{0=>[1, 3], 2=>[2], 1=>[5, 4, 6]} Benchmark testing: Benchmark.bm do |x| x.report { n.times { a.group_by{|x|x}.group_by{|x,y| y.size-1}.each_with_object({}){|(k,v),h| h[k] =v.flatten.uniq } }} x.report { n.times { Hash[a.group_by{|x|a.count(x)-1}.map{|k,v|[k,v.uniq]}] }} x.report { n.times { a.group_by{|x|a.count(x)-1}.each_with_object({}){|(k,v),h|h[k]=v.uniq} }} end n = 50000 user system total real 0.920000 0.000000 0.920000 ( 0.943945) 0.730000 0.000000 0.730000 ( 0.732669) 0.740000 0.000000 0.740000 ( 0.742029) n = 500000 user system total real 9.040000 0.000000 9.040000 ( 9.062923) 7.330000 0.000000 7.330000 ( 7.382192) 7.070000 0.000000 7.070000 ( 7.085146) -- Posted via http://www.ruby-forum.com/.