----- Original Message ----- From: "sekhar kavuru" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, June 01, 2006 2:19 AM
Subject: Analyzing perl array to extract maximum occuring element


How to identify a  maximum occuring element in a Perl Array

 e.g  @array values are  CMSIF CMSIF CMSFV

 Since CMSIF has two occurences , I need to select CMSIF

Maybe exist better solving but this brutte force must work ;-)

my $hash;
my $maxvalue = 0;
my $maxkey = '';
foreach my $a(0..$#array) {
   if(defined $hash{$array[$a]}) {
       $hash{$array[$a]} += 1;
       if($hash{$array[$a]} > $maxvalue) {
           $maxvalue = $hash{$array[$a]};
           $maxkey = $array[$a];
           }
       }
   else {
       $hash{$array[$a]} = 1;
       }
   }
print 'Max occurence is: ', $maxkey, ' (', $maxvalue, ")\n";

Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail from another non-spammer site please.)


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to