Logo

Programming-Idioms

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

Idiom #373 Size of set

Set n to the number of elements of the set x.

n = len(x)
int n (x.size());
final aSet = Set();
// omitted: add elements to aSet
final n = aSet.length;
n := len(x)
let n = x.size
int n = x.size();
generic function Card<T>(const ASet: T): Integer;
var
  Bytes: array [0..31] of byte;absolute ASet;
  i: Integer;
begin
  if GetTypeKind(T) <> tkSet then
     Exit(-1);
  if (SizeOf(ASet) > 32) then
     Exit(-1);
  Result := 0;
  for i := 0 to SizeOf(T) - 1 do
    Result := Result + PopCnt(Bytes[i]);
end;
begin
  n:=Card(X);
end.
n = x.size
let n = x.len();

New implementation...