Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #246 Count distinct elements

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

using System.Linq;
int c = items.Distinct().Count();
var c = items.toSet().length;
c = items |> Enum.uniq |> length
distinct := make(map[T]bool)
for _, v := range items {
	distinct[v] = true
}
c := len(distinct)
import Data.List
c = count . nub $ items
const c = new Set(items).size;
long c = items.stream().distinct().count();
val c = items.distinct().size
$c = count(array_unique($items));
uses classes;
function CountUniqueItems(Items: TStrings): Integer;
var
  List: TStringList;
begin
  List := TStringList.Create;
  List.Duplicates := dupIgnore;
  List.Sorted := True;
  List.AddStrings(Items);
  Result := List.Count;
  List.Free;
end;

begin
 ...
 c := CountUniqueItems(items);
 ...
end.
use List::Util qw(uniq);
my $c = scalar(uniq @items);
c = len(set(items))
c = items.uniq.count
use itertools::Itertools;
let c = items.iter().unique().count();

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