in reply to Re: •Re: Most frequent element in an array.
in thread Most frequent element in an array.

Thanks for following up on my chatterbox inquiry!

This is how I tried to use most_frequent().

my @array= qw ( this that the the other this the off ); print most_frequent(@array),"\n"; sub most_frequent { local *_=*_; $_[$_{$_}] = $_ for map{$_{$_}++; $_} @_; $_[-1]; }
The result printed is off instead of the expected the. Am I missing something?

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