Course of Raku / Advanced / More about built-in types / Sets, bags, and mixes / Exercises / Count the distinct

Solution: Count the distinct

Here is a possible solution to the task.

Code

my @data = 3, 1, 4, 1, 5, 9, 2, 6, 5, 3;

say set(@data).elems;

🦋 You can find the source code in the file count-the-distinct.raku.

Output

7

Comments

  1. Passing the array to set builds a set of its values. Because a set keeps only distinct values, the repeated numbers collapse into one.

  2. The elems method then returns the number of distinct values, which is 7.

Course navigation

Count the distinct   |   Common elements