From: Robert Klemme Date: 2012-01-23T19:43:41+09:00 Subject: Re: uniq with count; better way? On Mon, Jan 23, 2012 at 11:15 AM, Karsten Meier wrote: > If your data items are integers, and from a rather small range (compared > to computer memory...), then you can use an array instead of an hash: > > maxval = 10 > result = Array.new(maxval+1, 0) > ar.each{ >  |x| result[x] += 1; > } > > This returns an array and not an hash. > [0, 0, 0, 0, 2, 2, 3, 1, 0, 0, 0] > > To make a histogram, that data structure is even better. Otherwise you > need to transform it to a hash again. But for large data sets I still > expect it  to be faster: Don't expect, measure. There's Benchmark... > Your cpu  does not need to calculate a hash key of every single data > item, because the data item is already a perfect key for the array. Also > no hash key collisions can occur. But if only few of the numbers in the range are used you waste a potentially large Array for just a few entries. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/