Moved logic of collapsing songs into albums to model package

(it should really be called domain.... maybe will rename it later)
This commit is contained in:
Deluan
2022-12-19 11:00:20 -05:00
committed by Deluan Quintão
parent e03ccb3166
commit 28e7371d93
13 changed files with 507 additions and 26 deletions
+35
View File
@@ -0,0 +1,35 @@
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
}