Last active
August 16, 2017 13:31
-
-
Save arxdsilva/7392013cbba7a7090cbcd120b7f5ca31 to your computer and use it in GitHub Desktop.
Comparing elements of two string slices in golang
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
// 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