Course of Raku / Addendum 🆕 / Working with data / Lists, arrays, and hashes / Exercises / The second-largest value

Solution: The second-largest value

Here is a possible solution to the task.

Code

my @numbers = 3, 9, 4, 9, 1, 7;

my @distinct = @numbers.Set.keys.sort({ +$_ });

say @distinct[*-2];

🦋 You can find the source code in the file second-largest.raku.

Output

7

Comments

  1. Turning the list into a Set with .Set throws away the duplicate 9, since a set holds each value only once.

  2. .keys gives the distinct values back (as strings), so .sort({ +$_ }) sorts them numerically. The second-largest is then one place before the end, [*-2].

Course navigation

The second-largest value   |   Split into even and odd