Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

find maximum occuring element in a array

by capriguy84 (Novice)
on Sep 07, 2011 at 16:17 UTC ( [id://924618]=perlquestion: print w/replies, xml ) Need Help??

capriguy84 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a module or function available to parse an array and return the maximum occuring element in a list? Example: @array = qw( QS DD DFD SD QS QS QS DD DFS) How can I get QS as result? The elements cannot be put as a dictionary to set counters against them, so is there any ready solution available rather than parsing one by one and getting the result? To add to that, if there is a tie, it should return all the values tied. Thanks,
  • Comment on find maximum occuring element in a array

Replies are listed 'Best First'.
Re: find maximum occuring element in a array
by ikegami (Patriarch) on Sep 07, 2011 at 17:58 UTC
    use feature qw( say ); use List::Util qw( max ); ++$counts{$_} for @codes; my $max = max values(%counts); say grep $counts{$_} == $max, keys(%counts);
Re: find maximum occuring element in a array
by AR (Friar) on Sep 07, 2011 at 16:32 UTC

    If you're looking for the mode of a set and you've been handed the set (meaning you weren't involved in building it) then you're going to have to iterate over each element and count. There's a quantum algorithm to do this more quickly, but the tech isn't there yet.

    Why can't the elements be "put as a dictionary to set counters against them"? That's going to be the solution that most people hear will drive you towards. If you can't directly $count{thing}++ then you're going to have to $count{stringify(thing)}++ and find the mode(s) afterward.

      Thanks. How can I inspect the count of each element while building the array? I am using 'push' to create the array by reading from a CSV. I guess I would then have to dynamically create a dictionary and then set counter against each.

        Do it while you're pushing. You can run several lines of code on each piece of data or add each piece to multiple data structures. You can do whatever you want in order to get where you're going.

Re: find maximum occuring element in a array
by zentara (Archbishop) on Sep 07, 2011 at 16:59 UTC
    How can I get QS as result?

    Looking for somethings like this? The List::Utils may also have a method for strings

    #!/usr/bin/perl use strict; use warnings; my @strings = qw( QS DD DFD SD QS QS QS DD DFS); my %count; $count{ $_ }++ for @strings; my @sorted = (sort {$count{$a} cmp $count{$b}} keys %count); print pop @sorted, "\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      my @sorted = (sort {$count{$a} cmp $count{$b}} keys %count);

      $count{$a} and $count{$b} contain numbers so that should be:

      my @sorted = sort {$count{$a} <=> $count{$b}} keys %count;

      And you could accomplish the same effect with just one loop:

      $ perl -le' my @strings = qw( QS DD DFD SD QS QS QS DD DFS ); my ( %count, @max ); for ( @strings ) { $count{ $_ }++; $max[ 0 ] < $count{ $_ } and @max = ( $count{ $_ }, $_ ); } print $max[ 1 ]; ' QS
Re: find maximum occuring element in a array
by SuicideJunkie (Vicar) on Sep 07, 2011 at 16:24 UTC

    The only way to find the maximum element of a list is to inspect every one to make sure it is not bigger than the element you're currently thinking of.

    A for loop would do nicely. Track your best result, and a count of how many times you have seen it. Then return (($best) x $count);

    Edit: Of course, if by maximum you don't mean dictionary order where QS would come last, ignore this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://924618]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found