Logo

Programming-Idioms

Determine the number c of elements in the list x that satisfy the predicate p.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
  
c=count(p(x))
c := 0
for _, v := range x {
	if p(v) {
		c++
	}
}
func count[T any](x []T, p func(T) bool) int {
	c := 0
	for _, v := range x {
		if p(v) {
			c++
		}
	}
	return c
}
let c = x.filter(p).length
import java.util.ArrayList;
import java.util.Collections;
int c = Collections.frequency(x, p);
  c := 0;
  for el in x do if p(el) then Inc(c);
my $c = grep { p($_) } @x;
c = sum(p(v) for v in x)
c = x.count{|el| p(el) }