I must say, it makes me very happy to read to the end of one of these threads and find that the questioner has troubled to come back and say "thankyou". If only you had a user name and I could vote for your node (if that sounds garbled or faintly suggestive, then I encourage you to join the monastery, when all will become clear, if not All). My take on this Q is the following which may have some time advantage with long arrays because it doesn't count all the elements. Perhaps one loses the advantage on the sort, though. I'd be interested if anybody knows.
my $max = 0;
my $pop;
my $cnt = 0;
sort @ary;
while ($cnt < @ary) {
if ($ary[$cnt] eq $ary[$cnt+$max]) {
$pop = $ary[$cnt];
while ($ary[$cnt] eq $ary[$cnt+$max]) {
$max++;
}
$cnt += $max
}
else {
$cnt ++;
}
}
print "$pop, the most popular element, appears $max times";
§ George Sherston | [reply] [d/l] [select] |