From: Alder Green Date: 2006-06-05T15:31:23+09:00 Subject: Re: mode On 6/5/06, Elliot Temple wrote: > I bet there's a more elegant way to write this. Anyone want to show me? > > def mode(li) > best_num = nil > best_count = -1 > li.uniq.each do |item| > count = (li.select { |item2| item == item2}).length > if count > best_count > best_count = count > best_num = item > end > end > best_num > end > > > -- Elliot Temple > http://www.curi.us/blog/ > > > > > li.uniq.collect {|i| [li.select {|j| j == i}.size, i]}.sort.last.last -- -Alder