in reply to Re: finding the most popular in array
in thread finding the most popular in array

I liked your way of doing it, since it's two passes and the original question only wanted the most popular. Much better than sorting.

The other day I caught myself coding this same thing (boiling down a larger problem) but with this variation:

$hash{$_}++ for @array; %hash=reverse %hash; for (keys %hash) { $max = $_ > $max ? $_ : $max }; print $hash{$max}, "\n";
Thank goodness for TIMTOWDI.

Replies are listed 'Best First'.
Re: Re: Re: finding the most popular in array
by Anonymous Monk on Sep 10, 2001 at 22:26 UTC
    It could be done in one loop too.
    @a= qw(a b c d e c d e c d e c d e c d e a b c d a b c d a b c d a b c + d a b a b a e e d a a a a a a a a); for(@a) { if ($c<$r{$_}++) { $m=$_; $c=$r{$_} } }
    And this is to check.
    for (keys %r){print "$_ $r{$_}\n"}; print "\n\$m is $m\n\$c is $c";
    Hopes
    I hope you had enjoyed it