Created
February 11, 2015 17:52
-
-
Save walm/1ce1d7ca728cd4bfccd4 to your computer and use it in GitHub Desktop.
Unique values from slice of string
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
// in Go Play: http://play.golang.org/p/G5e-E8fe7h | |
package main | |
import "fmt" | |
func UniqStrs(slice []string) []string { | |
m := map[string]bool{} | |
for _, v := range slice { | |
m[v] = true | |
} | |
r := make([]string, len(m)) | |
i := 0 | |
for v := range m { | |
r[i] = v | |
i++ | |
} | |
return r | |
} | |
func main() { | |
v := []string{ | |
"hello", | |
"kalle", | |
"test", | |
"hello", | |
"Kalle", | |
"wow", | |
"hello", | |
} | |
fmt.Println("Values %v", UniqStrs(v)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment