# Locate maximum occuring element array in perl

guys..i need help...

how to extract a maximum occuring element in array using perl...

example...

a[1]=11;
a[2]=12;
a[3]=13;
a[4]=14;
a[5]=15;
a[6]=13;
a[7]=14;
a[8]=14;

we will see that, 14 is the most occuring at element 8...
how to know the no of frequently it occur?example above 14 is 3...

I'm not quite sure what you're asking-- you want to know that 14 is the most occurring element, but then you say "at 8", which implies that you also want to know the index at which it becomes true? I'm not sure.

Also, this isn't quite Perl-- or, not good Perl. For one, you don't have $signs before each array element, and you're also using a variable name of "a", which is a special variable (actually,$a is special, @a isn't, but it's not a good idea to use them). Also, you start your array at 1, rather than 0, which means that you want to skip undefined values?

Either way, this is simple with hashes. For your example:

Code:
#!perl
my(@a) = (undef,11,12,13,14,15,13,14,14);
my(%count);
foreach my $value (@a) {$count{$value}++; }$max_value = (sort {$count{$b} <=> $count{$a}} @a)[0];
print "Max value = $max_value, occurrs$count{$max_value} times\n"; If you want to know the index at which this happened, it's a little more involved, but not too hard: Code: #!perl my(@a) = (undef,11,12,13,14,15,13,14,14); my(%count,$max);
for(my $i=1;$i<@a;$i++) {$count{$a[$i]}++;
if($count{$a[$i]} >$count{$a[$max]}) { $max =$i; }
}
print "Max value = $a[$max], occurrs $count{$a[$max]} times, surpasses at index$max\n";
DaveE

thanks davee...

all im saying is just an example...im not give the proper example which is im sorry...

im just wondering...

how if im want to print all the element in that array with showing the max occur...

like this...

my(@a) = (undef,11,12,13,14,15,13,14,14);

11= 1 occurence
12=1 occurence
13=2 occurence
14=3 occurence
15=1 occurence

im still new in perl...need ur help guys...

thanks davee...

all im saying is just an example...im not give the proper example which is im sorry...

im just wondering...

how if im want to print all the element in that array with showing the max occur...

like this...

my(@a) = (undef,11,12,13,14,15,13,14,14);

11= 1 occurence
12=1 occurence
13=2 occurence
14=3 occurence
15=1 occurence

im still new in perl...need ur help guys...

The above code is already tracking that information in the hashes. This part right here:

Code:
foreach my $value (@a) {$count{$value}++; } That takes each value in the @a array, and assigns it to an entry in the %count hash. Each time the value is encountered in the array, it increments the count of that value by 1. After that loop is complete, you can reference$count{12} and it will be set to 1, or $count{14} and it will be set to 3. So you can loop through a series of values and print out (for instance)$count{$value}, and it will print out the number of occurrences of each element. If you don't want to print out any instances that occur 0 times, you can either wrap them in an if statement, or you can iterate over the (keys %count) array, which will ONLY return the values that have occurrences in the hash. DaveE The above code is already tracking that information in the hashes. This part right here: Code: foreach my$value (@a) {
$count{$value}++;
}
That takes each value in the @a array, and assigns it to an entry in the %count hash. Each time the value is encountered in the array, it increments the count of that value by 1. After that loop is complete, you can reference $count{12} and it will be set to 1, or$count{14} and it will be set to 3. So you can loop through a series of values and print out (for instance) $count{$value}, and it will print out the number of occurrences of each element.

If you don't want to print out any instances that occur 0 times, you can either wrap them in an if statement, or you can iterate over the (keys %count) array, which will ONLY return the values that have occurrences in the hash.

DaveE

if i want to print out the occurence more than 2 times only,u said i can iterate over the (keys %count) array...

i still don get it...

is it like this...

if (keys %count > 2){print $count{$value};}

if i want to print out the occurence more than 2 times only,u said i can iterate over the (keys %count) array...
It would be like:

Code:
foreach my $value (keys %count) { if($count{$value} > 2) { print "$value occurred $count{$value} times!\n";
}
}
DaveE