How to get intersection of two slice in golang?

前端 未结 5 1512
不思量自难忘°
不思量自难忘° 2021-02-18 22:24

Is there any efficient way to get intersection of two slices in Go?

I want to avoid nested for loop like solution
slice1 := []string{\"foo\", \"bar\",\"hello\"}         


        
5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-18 22:58

    https://github.com/viant/toolbox/blob/master/collections.go

    Another O(m+n) Time Complexity solution that uses a hashmap. It has two differences compared to the other solutions discussed here.

    • Passing the target slice as a parameter instead of new slice returned
    • Faster to use for commonly used types like string/int instead of reflection for all

提交回复
热议问题