Is there any efficient way to get intersection of two slices in Go?
I want to avoid nested for loop like solutionslice1 := []string{"foo", "bar","hello"}
slice2 := []string{"foo", "bar"}
intersection(slice1, slice2)
=> ["foo", "bar"]
order of string does not matter
How do I get the intersection between two arrays as a new array?
A
to each in B
(O(n^2)
)O(n)
)A
and do an optimized intersection (O(n*log(n))
)All of which are implemented here
https://github.com/juliangruber/go-intersect
It's a best method for intersection two slice. Time complexity is too low.
Time Complexity : O(m+n)
m = length of first slice.
n = length of second slice.
func intersection(s1, s2 []string) (inter []string) {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
// If elements present in the hashmap then append intersection list.
if hash[e] {
inter = append(inter, e)
}
}
//Remove dups from slice.
inter = removeDups(inter)
return
}
//Remove dups from slice.
func removeDups(elements []string)(nodups []string) {
encountered := make(map[string]bool)
for _, element := range elements {
if !encountered[element] {
nodups = append(nodups, element)
encountered[element] = true
}
}
return
}
if there exists no blank in your []string
, maybe you need this simple code:
func filter(src []string) (res []string) {
for _, s := range src {
newStr := strings.Join(res, " ")
if !strings.Contains(newStr, s) {
res = append(res, s)
}
}
return
}
func intersections(section1, section2 []string) (intersection []string) {
str1 := strings.Join(filter(section1), " ")
for _, s := range filter(section2) {
if strings.Contains(str1, s) {
intersection = append(intersection, s)
}
}
return
}
simple, generic and mutiple slices ! (Go 1.18)
Time Complexity : may be linear
package main
import (
"fmt"
"golang.org/x/exp/constraints"
)
func interSection[T constraints.Ordered](pS ...[]T) (result []T) {
hash := make(map[T]*int) // value, counter
for _, slice := range pS {
for _, value := range slice {
if counter := hash[value]; counter != nil {
*counter++
} else {
i := 1
hash[value] = &i
}
}
}
result = make([]T, 0)
for value, counter := range hash {
if *counter >= len(pS) {
result = append(result, value)
}
}
return
}
func main() {
slice1 := []string{"foo", "bar", "hello"}
slice2 := []string{"foo", "bar"}
fmt.Println(interSection(slice1, slice2))
// [foo bar]
ints1 := []int{1, 2, 3, 9}
ints2 := []int{10, 4, 2, 4, 8, 9}
ints3 := []int{2, 4, 8, 1}
fmt.Println(interSection(ints1, ints2, ints3))
// [2 4]
}
playground : https://go.dev/play/p/AFpnUvWHV50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With