vkk05 has asked for the wisdom of the Perl Monks concerning the following question:
Output:use strict; use warnings; use Data::Dumper; my @a = ( 2,2,2,3,3,4,4 ); my (%hash, %count); my $prev; foreach ( 0..$#a ) { my $cur = $a[$_]; warn "cur:$cur\n"; if ( ($prev && $cur != $prev || $_ == $#a) ) { my ($prev_key) = keys %count; if ( !$prev_key ||( $count{$prev_key} ) <= $hash{$prev} ) { delete $count{$prev_key} if $prev_key; $count{$prev} = $hash{$prev}; } } $hash{$cur}++; $prev = $cur; } warn Dumper \%count;
The answer should be '3'. Since element '2' has occured consecutively '3' times in the array.$VAR1 = { '2' => 3 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Print max count of repeated consecutive numbers in an array
by Corion (Patriarch) on Oct 06, 2021 at 12:47 UTC | |
|
Re: Print max count of repeated consecutive numbers in an array
by tybalt89 (Monsignor) on Oct 06, 2021 at 17:37 UTC | |
|
Re: Print max count of repeated consecutive numbers in an array
by kcott (Archbishop) on Oct 07, 2021 at 00:09 UTC | |
|
Re: Print max count of repeated consecutive numbers in an array
by tybalt89 (Monsignor) on Oct 06, 2021 at 15:05 UTC | |
by LanX (Saint) on Oct 06, 2021 at 15:40 UTC | |
|
Re: Print max count of repeated consecutive numbers in an array
by jo37 (Curate) on Oct 06, 2021 at 17:28 UTC |