in reply to Re: find maximum occuring element in a array
in thread find maximum occuring element in a array

my @sorted = (sort {$count{$a} cmp $count{$b}} keys %count);

$count{$a} and $count{$b} contain numbers so that should be:

my @sorted = sort {$count{$a} <=> $count{$b}} keys %count;

And you could accomplish the same effect with just one loop:

$ perl -le' my @strings = qw( QS DD DFD SD QS QS QS DD DFS ); my ( %count, @max ); for ( @strings ) { $count{ $_ }++; $max[ 0 ] < $count{ $_ } and @max = ( $count{ $_ }, $_ ); } print $max[ 1 ]; ' QS

Replies are listed 'Best First'.
Re^3: find maximum occuring element in a array
by AnomalousMonk (Archbishop) on Sep 07, 2011 at 21:14 UTC