From 84cc7a9e05b3cc120146fbbfe51250a89e61aaeb Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 7 Sep 2022 22:10:41 +0200 Subject: [PATCH] Count and CountCmp added --- count.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 count.go diff --git a/count.go b/count.go new file mode 100644 index 0000000..27d9e00 --- /dev/null +++ b/count.go @@ -0,0 +1,15 @@ +package slices + +func Count[T comparable](slice []T, value T) int { + return CountCmp(slice, value, DefaultEqualityComparator[T]) +} + +func CountCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int { + c := 0 + for _, v := range slice { + if cmp(v, value) { + c++ + } + } + return c +}