in reply to Re^2: unxpected sort warnings while using sort
in thread unxpected sort warnings while using sort
#!/usr/bin/perl use warnings; use strict; sub most_duplicated { my @numbers = @_; my %count; ++$count{$_} for @numbers; my @most_duplicated; my $max_count = 0; for my $n (keys %count) { if ($count{$n} > $max_count) { @most_duplicated = ($n); $max_count = $count{$n} } elsif ($count{$n} == $max_count) { push @most_duplicated, $n; } } return ($max_count, \@most_duplicated) } my @data = qw( 2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1 4 5 3 + 3 3 ); my ($count, $most_duplicated) = most_duplicated(@data); print "@$most_duplicated occurred $count times.\n";
It's also possible to avoid two loops and do all the work in one loop, but I think this way is more understandable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: unxpected sort warnings while using sort
by perlynewby (Scribe) on Jul 30, 2015 at 20:49 UTC | |
by perlynewby (Scribe) on Jul 31, 2015 at 23:17 UTC |