Created
February 15, 2019 10:17
-
-
Save snebel29/d71ccde98377ab761e33e7d197238ac8 to your computer and use it in GitHub Desktop.
slice of ints intersection in GO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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