Course of Raku / Addendum 🆕 / Working with data / Lists, arrays, and hashes / Exercises / The most common element

Solution: The most common element

Here is a possible solution to the task.

Code

my @values = <a b a c a b>;

my %count;
%count{$_}++ for @values;

say %count.sort(-*.value)[0].key;

🦋 You can find the source code in the file most-common.raku.

Output

a

Comments

  1. After tallying each value in %count, sorting the pairs by -*.value puts the most frequent first; [0].key then gives back that element.

Course navigation

The most common element   |   Zip two lists into a hash