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
7Comments
Turning the list into a
Setwith.Setthrows away the duplicate9, since a set holds each value only once..keysgives the distinct values back (as strings), so.sort({ +$_ })sorts them numerically. The second-largest is then one place before the end,[*-2].