#! perl -slw use strict; my @array = map{ int rand 10 } 1 .. 100; my %counter; $counter{$_} ++ for @array; my $most_frequent = ( sort{ $b->[1] <=> $a->[1] } map{ [ $_, $counter{$_} ] } keys %counter )[0]->[0]; print $most_frequent;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Most frequent element in an array.
by merlyn (Sage) on Feb 16, 2003 at 07:28 UTC | |
-- Randal L. Schwartz, Perl hacker
| [reply] [d/l] |
by BrowserUk (Patriarch) on Feb 16, 2003 at 09:02 UTC | |
Agreed. Though the use of the ST wasn't to reduce the cost of sorting, just a mechanism of grouping the key/value pairs, so that I could sort the values and retain the associating with the respective key. A linear search is all that is needed. That said, petruchio gave an alternative in the CB which I had to look at several times to understand, but which I think is particularly neat.
I hope he'll forgive me for pushing this one step further with this sub which I have added to my personal utilities module.
Which goes along way to providing, and could easliy be extended to provide most if not all of the function available in the Statistics::Frequency module I saw mentioned, without the overhead of the 50 or so lines of inefficient and frankly rather pedestrian code that make it up. I find it incredulous that the author implemented a complete function and a nested loop to determine the "sum of the frequencies", which unless I am just too tired, amounts to the size of the list or array? Just goes to show that you have to read the source before blythly accepting the merit of any given module. Just being a part of CPAN isn't of itself enough to ensure any sort of quality. Examine what is said, not who speaks. The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead. | [reply] [d/l] [select] |
by dug (Chaplain) on Feb 16, 2003 at 18:46 UTC | |
That is a nice little snippet. I hope he'll forgive me for pushing this one step further with this sub which I have added to my personal utilities module.
I don't think that does what you want it to do. It only returns the most frequent element if the frequency is greater than or equal to the last index of the array. For instance, if pass that function the list qw( 1 1 2 3 );, $_[2] is set to 1, and $_[1] is set to 2 then 3. But $_[3] remains 3, and your code will return it. Which goes along way to providing, and could easliy be extended to provide most if not all of the function available in the Statistics::Frequency module I saw mentioned, without the overhead of the 50 or so lines of inefficient and frankly rather pedestrian code that make it up. This inefficient and pedestrian code you speak of is much more efficient than the broken code you posted. First I tried a one element list so your code couldn't break. Then I disregarded the fact that it breaks, and tried a slightly larger list. The results look good for Statistics::Frequency. I find it incredulous that the author implemented a complete function and a nested loop to determine the "sum of the frequencies", which unless I am just too tired, amounts to the size of the list or array? I assume that you mean that you are incredulous, or that you find it incredible. Just goes to show that you have to read the source before blythly accepting the merit of any given module. Just being a part of CPAN isn't of itself enough to ensure any sort of quality. Yeah, especially if they are written by the CPAN's master librarian and co-author of Mastering Algorithms with Perl. In fact, I think I should distrust anything released by that author. Guess I better go downgrade my Perl. -- dug | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Feb 17, 2003 at 11:02 UTC | |
by toma (Vicar) on Feb 16, 2003 at 18:28 UTC | |
This is how I tried to use most_frequent(). The result printed is off instead of the expected the. Am I missing something? Update: I have benchmarked merlyn's code, Petruchio's CB suggestion, and the Statistics::Frequency approach. The clear winner is merlyn's code. It is faster and uses less memory than the other approaches. It should work perfectly the first time! - toma | [reply] [d/l] [select] |