Skip to content

Instantly share code, notes, and snippets.

@snebel29
Created February 15, 2019 10:17
Show Gist options
  • Select an option

  • Save snebel29/d71ccde98377ab761e33e7d197238ac8 to your computer and use it in GitHub Desktop.

Select an option

Save snebel29/d71ccde98377ab761e33e7d197238ac8 to your computer and use it in GitHub Desktop.
slice of ints intersection in GO
package main
import (
"fmt"
)
func main() {
v := Intersection([]int{1,2,3,4,5,6}, []int{2,4,6})
fmt.Println(v)
}
func Intersection(a []int, b []int) []int {
set := make([]int, 0)
bufferSet := make(map[int]bool)
for i := 0; i < len(a); i++ {
bufferSet[a[i]] = true
}
for i := 0; i < len(b); i++ {
el := b[i]
if _, found := bufferSet[el]; found {
set = append(set, el)
}
}
return set
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment