in reply to finding the most popular in array

Thanks for all of your answers!

Thanks,
Ralph :)

Replies are listed 'Best First'.
Re: Re: finding the most popular in array
by George_Sherston (Vicar) on Sep 10, 2001 at 18:42 UTC
    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