Skip to content

Instantly share code, notes, and snippets.

@kaskavalci
Created July 19, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaskavalci/75ddd4423a77732180a3d12c0e80ea9a to your computer and use it in GitHub Desktop.
Save kaskavalci/75ddd4423a77732180a3d12c0e80ea9a to your computer and use it in GitHub Desktop.
Remove duplicates from a string array in go
func unique(tokens *[]string) {
v := reflect.ValueOf(tokens).Elem()
if v.Len() <= 1 {
return
}
sort.Strings(*tokens)
i := 0
for j := 1; j < v.Len(); j++ {
if (*tokens)[i] == (*tokens)[j] {
continue
}
// unique token, save it
i++
v.Index(i).Set(v.Index(j))
}
i++
v.SetLen(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment