Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #246 Count distinct elements

Set c to the number of distinct elements in the list items.

use List::Util qw(uniq);
my $c = scalar(uniq @items);
(def items [1 2 3 4 4 5 5 5])

(def c (count (set items)))

Converting a collection to a set removes any duplicate elements.

New implementation...
< >
programming-idioms.org