Locate maximum occuring element array in perl

In summary: It would be like:foreach my $value (keys %count) {if ($count{$value} > 2) {print "$value occurred $count{$value} times!\n";}}
  • #1
paanz
4
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
  • #2
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
 
  • #3
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 occurence
12=1 occurence
13=2 occurence
14=3 occurence
15=1 occurence

im still new in perl...need ur help guys...
 
  • #4
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 occurence
12=1 occurence
13=2 occurence
14=3 occurence
15=1 occurence

im still new in perl...need ur help guys...
 
  • #5
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
 
  • #6
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 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};}
 
  • #7
paanz said:
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
 

Related to Locate maximum occuring element array in perl

1. How can I find the maximum occurring element in an array using Perl?

In Perl, you can use the max function from the List::Util module to find the maximum value in an array. This function will return the highest value in the array, which can be the maximum occurring element if there are no repeated elements.

2. Is there a built-in function in Perl to locate the most frequent element in an array?

No, there is no built-in function in Perl specifically for finding the most frequent element in an array. However, you can use the max function along with other built-in functions like grep and sort to achieve this.

3. How do I handle ties when finding the maximum occurring element in an array using Perl?

If there are multiple elements with the same highest frequency, the max function will return the first occurrence. To handle ties, you can use the grep function to filter the array and then use the sort function to sort the filtered array and get the first element.

4. Can I use a hash to find the maximum occurring element in an array using Perl?

Yes, you can use a hash to find the maximum occurring element in an array. You can loop through the array and use the elements as keys in the hash and increment the corresponding values. Then, you can use the max function on the values of the hash to find the maximum occurring element.

5. Are there any other efficient methods for locating the maximum occurring element in an array using Perl?

Yes, there are other efficient methods for finding the maximum occurring element in an array using Perl. One approach is to use the List::MoreUtils module and its frequency function to get the frequency of each element in the array. Then, you can use the max function on the values returned by the frequency function to find the maximum occurring element.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
2
Views
1K
  • Nuclear Engineering
Replies
7
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
17K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
420
Back
Top