---
name: CountBy
slug: countby
sourceRef: slice.go#L849
category: core
subCategory: slice
playUrl: https://go.dev/play/p/5GMQP5vNT4q
variantHelpers:
  - core#slice#countby
similarHelpers:
  - core#slice#count
  - core#slice#every
  - core#slice#some
  - core#slice#filter
  - core#slice#find
  - core#slice#countbyerr
position: 0
signatures:
  - "func CountBy[T any](collection []T, predicate func(item T) bool) int"
---

Counts the number of elements for which the predicate is true.

```go
lo.CountBy([]int{1, 5, 1}, func(i int) bool {
    return i < 4
})
// 2
```


