Skip to content

Instantly share code, notes, and snippets.

@arxdsilva
Last active August 16, 2017 13:31
Show Gist options
  • Save arxdsilva/7392013cbba7a7090cbcd120b7f5ca31 to your computer and use it in GitHub Desktop.
Save arxdsilva/7392013cbba7a7090cbcd120b7f5ca31 to your computer and use it in GitHub Desktop.
Comparing elements of two string slices in golang
// You can test here: https://play.golang.org/p/zTaNla0oXJ
package main
import (
"fmt"
)
func main() {
x := []string{"a","b","c"}
y := []string{"c", "d"}
//z := []string{"c", "d"}
w := compare(y, x)
fmt.Println(w)
}
func compare(a, b []string) []string {
for i := len(a) - 1; i >= 0; i-- {
for _, vD := range b {
if a[i] == vD {
a = append(a[:i], a[i+1:]...)
break
}
}
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment