39 lines
539 B
Go
39 lines
539 B
Go
|
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
|
||
|
|
||
|
}
|