in reply to Re: •Re: Most frequent element in an array.
in thread Most frequent element in an array.
This is how I tried to use most_frequent().
The result printed is off instead of the expected the. Am I missing something?my @array= qw ( this that the the other this the off ); print most_frequent(@array),"\n"; sub most_frequent { local *_=*_; $_[$_{$_}] = $_ for map{$_{$_}++; $_} @_; $_[-1]; }
Update: I have benchmarked merlyn's code, Petruchio's CB suggestion, and the Statistics::Frequency approach. The clear winner is merlyn's code. It is faster and uses less memory than the other approaches.
It should work perfectly the first time! - toma
|
|---|