tstor/pkg/slicesutils/intersections.go

39 lines
539 B
Go
Raw Normal View History

2024-06-14 22:14:44 +00:00
package slicesutils
func Intersection[T comparable](slices ...[]T) []T {
counts := map[T]int{}
result := []T{}
for _, slice := range slices {
for _, val := range slice {
counts[val]++
}
}
for val, count := range counts {
if count == len(slices) {
result = append(result, val)
}
}
return result
}
func IntersectionFunc[T any](s1 []T, s2 []T, cmp func(T, T) bool) []T {
set := make([]T, 0)
for _, a := range s1 {
for _, b := range s2 {
if cmp(a, b) {
set = append(set, a)
}
}
}
return set
}