From: Erik Veenstra Date: 2008-10-01T20:29:32+09:00 Subject: Re: Mode method for Array And since we all love speed, we tend to avoid inject. (For those who don't know: inject and inject! are really, really slow. I mean, _really_ slow...) Speed is also the reason for "pre-defining" variables used in iterations. (6% faster!!) For low level methods like these, speed is much more important than readability. And the inject versions of the methods below aren't even more readable than the faster implementations. So, we'll go for the fast ones: module Enumerable def mode empty? ? [] : frequencies.group_by_value.max.last end def frequencies x = nil res = Hash.new(0) each{|x| res[x] += 1} res end end class Hash def group_by_value k = v = nil res = {} each{|k, v| (res[v] ||= []) << k} res end end gegroet, Erik V. - http://www.erikveen.dds.nl/