Eustaquio 'TaQ' Rangel
9/30/2008 10:15:00 PM
> I'd like to write a get_mode method for the Array class. The method would return an array of the most frequently occurring element or elements.
> So [3, 1, 1, 55, 55].get_mode would return [1, 55].
> I have a way to do this but I don't know if it's the best way. I was wondering if anyone had any suggestions?
What is your way? Maybe we can have some idea of what parameters you are using
to the the most frequently elements. Using something like
irb(main):001:0> [3,1,1,55,55].inject(Hash.new(0)){|memo,item| memo[item] += 1;
memo}.sort_by {|e| e[1]}.reverse
=> [[55, 2], [1, 2], [3, 1]]
can return you some elements ordered by frequency.