in reply to Print max count of repeated consecutive numbers in an array
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11137256 use warnings; my @a = ( 2,2,2,3,3,4,4 ); my @best = (my $count = 1, my $prev = shift @a); for my $next ( @a ) { $prev == $next or $prev = $next, $count = 0; $best[ ++$count ] = $next; } print "highest count is $#best for the number $best[-1]\n";
Outputs:
highest count is 3 for the number 2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print max count of repeated consecutive numbers in an array
by LanX (Saint) on Oct 06, 2021 at 15:40 UTC |