Peter’s blog ✴ Week 378 ✴ 15 June 2026

THE WEEKLY CHALLENGE
Second and sum

The Perl Camel

Task 1

Second largest digit

You are given an alphanumeric string. Write a script to find the second largest distinct digit in the given string. Return -1 if none found.

Examples


Example 1
Input: $str = 'aaaaa77777'
Output: -1
The only digit in the given string is 7 and there is no
   second digit.

Example 2
Input: $str = 'abcde'
Output: -1
No numerical digits in the given string.

Example 3
Input: $str = '9zero8eight7seven9'
Output: 8

Example 4
Input: $str = 'xyz9876543210'
Output: 8

Example 5
Input: $str = '4abc4def2ghi8jkl2'
Output: 4

Analysis

My solution starts with very one-liner-ish statement:

@digits = sort {$b <=> $a} grep {/\d/} split(//, $string);

Reading that right to left, it splits the string into an array of characters, greps the characters that are digits, and sorts them largest to smallest into the array @digits.

So for example '9zero8eight7seven9' yields (9, 9, 8, 7).

The number we are looking for is the first element of @digits that is smaller than $digits[0], which might not be $digits[1] if the value in $digits[0] is repeated in $digits[1] ....

This maybe isn't the fastest solution, because it sorts the whole @digits array, whereas a single scan along the array could derive the answer. But I tried my solution with a million random digits and various other difficult combinations including 999,999 ones followed by a single zero, and none took more tha 2 seconds, so I think this is good enough.

Try it 

Your input:



eg: a9s8d7f6g5h4j3

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-06-15
use utf8;     # Week 378 - task 1 - Second largest digit
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

second_largest_digit('xy4mn7oo5');
second_largest_digit('999888');
second_largest_digit('21');
second_largest_digit('Weekly Challenge 378 - task 1 - 2026-06-15');
second_largest_digit('1');
second_largest_digit('q');
second_largest_digit('');

# bigger example
my $string;
$string .= substr('abcdefghijklmnopqrstuvwxyz0123456789', 
    rand(36), 1) for 0 .. 500;
second_largest_digit($string);

sub second_largest_digit {
    
    my ($string, @digits, $j);
    
    # initialise
    $string = $_[0];
    say qq[\nInput:  '$string'];
    
    # extract digits and reverse sort them 
    @digits = sort {$b <=> $a} grep {/\d/} split(//, $string);
    
    # find the first one less than $digits[0]
    for $j (1 .. $#digits) {
        if ($digits[$j] < $digits[0]) {
            say qq[Output: $digits[$j]];
            return;
        }
    }
    
    # else
    say qq[Output: -1];         
}

10 lines of code
Completed after the closing date and not submitted to GitHub

Output from script


Input:  'xy4mn7oo5'
Output: 5

Input:  '999888'
Output: 8

Input:  '21'
Output: 1

Input:  'Weekly Challenge 378 - task 1 - 2026-06-15'
Output: 7

Input:  '1'
Output: -1

Input:  'q'
Output: -1

Input:  ''
Output: -1

Input: 
   '7xlw1xylcb4d17rgowhndtoq3gryy0vd4ee7lqyud2v5p55l5d6dcerolmefvyf47k
   32pqsdt3sa975hlq35gvmm8do0zyqnoyw8tr8g2mhal50g9yhvwco6gtsukcrjdqkzz
   p2o48k8e3ea8k3u5a3mr2m8bqepqsz5yphud5ydq8sqr1lb9pb4o4ewbb1amtgcaj8f
   9bkxjx2vskgokniu8hzqnizs2qh6aczs9604g751595n978vq6kxrmks8v7go45sydk
   f1ahmemiv2y6606pllez39u4h7yvz9yclhsmhmkir2t3zzkvh5wwe4tuh00mqpliewx
   p3vibqlcljoygn89t9ooqpv10i28u6q9zkddmg3kiblk2xej5vgns58nz2x0bi5oe6n
   89uc2t9ksdcytvyrmkn5df1h7xbboyn659qcr5ss78kixcbj2ylzb1clpqxnvag1mzh
   ot5rl1lzdvcwhm78wgpwnn17154a53qar'
Output: 8

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain