From: Michael Fellinger Date: 2006-06-06T17:20:10+09:00 Subject: Re: mode li.inject(Hash.new(0)){|s,v| s[v] += 1; s}.max.last On Monday 05 June 2006 16:23, Elliot Temple wrote: > 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/ -- EYE CONTACT Q: Why do men find it so difficult to make eye contact? A: Breasts don't have eyes.