navidrome/utils/slice/slice.go
Deluan 28e7371d93 Moved logic of collapsing songs into albums to model package
(it should really be called domain.... maybe will rename it later)
2022-12-28 15:31:56 -05:00

35 lines
560 B
Go

package slice
func Group[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T {
m := map[K][]T{}
for _, item := range s {
k := keyFunc(item)
m[k] = append(m[k], item)
}
return m
}
func MostFrequent[T comparable](list []T) T {
if len(list) == 0 {
var zero T
return zero
}
var topItem T
var topCount int
counters := map[T]int{}
if len(list) == 1 {
topItem = list[0]
} else {
for _, id := range list {
c := counters[id] + 1
counters[id] = c
if c > topCount {
topItem = id
topCount = c
}
}
}
return topItem
}