Locate maximum occuring element array in perl

Click For Summary
SUMMARY

The discussion focuses on extracting the maximum occurring element from an array in Perl, specifically using a hash to count occurrences. The provided code snippets demonstrate how to implement this functionality, including tracking the index of the maximum occurring element. The final solution allows for printing all elements along with their occurrence counts, and filtering to display only those that occur more than twice.

PREREQUISITES
  • Understanding of Perl syntax and data structures
  • Familiarity with arrays and hashes in Perl
  • Knowledge of control structures such as loops and conditionals
  • Basic debugging skills to troubleshoot Perl scripts
NEXT STEPS
  • Learn about Perl hashes and their applications in counting occurrences
  • Explore Perl's built-in functions for sorting and filtering data
  • Investigate advanced array manipulation techniques in Perl
  • Study error handling and debugging practices in Perl scripts
USEFUL FOR

Perl developers, data analysts, and anyone interested in efficient data processing and analysis using Perl.

paanz
Messages
4
Reaction score
0
guys..i need help...

how to extract a maximum occurring 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 occurring at element 8...
how to know the no of frequently it occur?example above 14 is 3...

really need your help...
 
Technology news on Phys.org
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 I am saying is just an example...im not give the proper example which is I am sorry...

btw...thanks ur answer...
im just wondering...

how if I am 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 occurrence
12=1 occurrence
13=2 occurrence
14=3 occurrence
15=1 occurrence

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

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

btw...thanks ur answer...
im just wondering...

how if I am 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 occurrence
12=1 occurrence
13=2 occurrence
14=3 occurrence
15=1 occurrence

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
 
davee123 said:
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 occurrence 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};}
 
paanz said:
if i want to print out the occurrence 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
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
17K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K