feat: add Frequency in slice package

This commit is contained in:
dudaodong committed 2024-09-06 15:06:35 +08:00
1 parent 90e5a0bfb2
commit c3372e18b1
5 files changed
+140 -8

No files matched your search

+12
View File
@@ -1386,3 +1386,15 @@ func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T {
return paddedSlice
}
// Frequency counts the frequency of each element in the slice.
// Play: todo
func Frequency[T comparable](slice []T) map[T]int {
result := make(map[T]int)
for _, v := range slice {
result[v]++
}
return result
}