#!/usr/bin/env perl use v5.36; use experimental qw; use builtin qw; use List::UtilsBy qw; # See blog post for shorter code using List::Categorize. sub task2 ( @a ) { my %h; for my ($k, $v) (indexed @a) { my $href = ( $h{$v} //= {} ); $href->{KEY } = $v; $href->{COUNT} += 1; $href->{FIRST} //= $k; $href->{LAST } = $k; } $_->{SPAN} = $_->{LAST} - $_->{FIRST} + 1 for values %h; my $best = min_by { $_->{FIRST} } min_by { $_->{SPAN } } max_by { $_->{COUNT} } values %h; return [ @a[ $best->{FIRST} .. $best->{LAST} ] ]; } my @tests = ( # From TWC examples: [ [ 1, 3, 3, 2 ] , [ 3, 3 ] ], [ [ 1, 2, 1, 3 ] , [ 1, 2, 1 ] ], [ [ 1, 3, 2, 1, 2 ] , [ 2, 1, 2 ] ], [ [ 1, 1, 2, 3, 2 ] , [ 1, 1 ] ], [ [ 2, 1, 2, 1, 1 ] , [ 1, 2, 1, 1 ] ], # TWC examples translated to alpha, for easier debugging. [ [qw] , [qw ] ], [ [qw] , [qw ] ], [ [qw] , [qw ] ], [ [qw] , [qw ] ], [ [qw] , [qw] ], # Numeric and alpha versions to stress tie-breaking rules: [ [qw<1 1 2 2 2 2 1 1> ] , [qw<2 2 2 2>] ], [ [qw ] , [qw] ], [ [qw<1 1 1 1 2 2 2 2> ] , [qw<1 1 1 1>] ], [ [qw ] , [qw] ], [ [qw<1 1 1 1 2 3 2 2 2> ] , [qw<1 1 1 1>] ], [ [qw ] , [qw] ], ); use Test::More; plan tests => 0+@tests; for (@tests) { my ( $in_aref, $expected_aref ) = @{$_}; is_deeply task2(@{$in_aref}), $expected_aref, "task2(qw<@{$in_aref}>)"; }