From: Elliot Temple Date: 2006-06-05T16:23:45+09:00 Subject: Re: mode On Jun 4, 2006, at 11:31 PM, Alder Green wrote: > li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last cool, thanks, collecting pairs works well. On Jun 4, 2006, at 11:40 PM, Daniel Sheppard wrote: > >> -----Original Message----- >> From: Elliot Temple [mailto:curi@curi.us] >> >> I bet there's a more elegant way to write this. Anyone want >> to show me? >> >> def mode(li) > ... >> end > > It would probably help if you said what you were trying to achieve > rather than asking people to try and interpret code that you admit > isn't > elegant. Mode means: (Statistics) the value that occurs most frequently in a given set of data. > class Array > def mode > count = Hash.new(0) > each {|x| count[x] += 1 } > count.sort_by{|k,v|v}.last[0] > end > end that's a nice way :) -- Elliot Temple http://www.curi.us/blog/