Locate maximum occuring element array in perl

Click For Summary

Discussion Overview

The discussion revolves around extracting the maximum occurring element in an array using Perl. Participants explore how to count occurrences of elements, identify the maximum, and print the results, including handling edge cases and improving code practices.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant seeks help to find the maximum occurring element in an array and its frequency, providing an example array.
  • Another participant questions the clarity of the original request and points out issues with the Perl syntax used in the example.
  • Code snippets are provided to demonstrate how to count occurrences using hashes in Perl.
  • Further inquiries are made about printing all elements with their occurrences, leading to additional code suggestions.
  • Participants discuss how to filter results to show only elements that occur more than a specified number of times.

Areas of Agreement / Disagreement

Participants generally agree on the approach of using hashes to count occurrences, but there is no consensus on the clarity of the original question or the best way to implement the desired functionality.

Contextual Notes

Some participants note potential issues with the initial example provided, such as array indexing starting at 1 instead of 0, and the use of variable names that may conflict with Perl's special variables.

Who May Find This Useful

Individuals learning Perl, particularly those interested in data manipulation and counting occurrences within arrays.

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
4K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · 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