royalcat
e9df8925d1
Some checks failed
docker / build-docker (linux/amd64) (push) Failing after 18s
docker / build-docker (linux/386) (push) Successful in 1m57s
docker / build-docker (linux/arm64) (push) Successful in 7m22s
docker / build-docker (linux/arm/v7) (push) Successful in 7m53s
docker / build-docker (linux/arm64/v8) (push) Failing after 3h2m18s
38 lines
539 B
Go
38 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
|
|
|
|
}
|