Skip to content

Instantly share code, notes, and snippets.

@William-Yeh
Created September 1, 2020 09:16
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 William-Yeh/9669c199480ab44cf560365ee0cb1d2e to your computer and use it in GitHub Desktop.
Save William-Yeh/9669c199480ab44cf560365ee0cb1d2e to your computer and use it in GitHub Desktop.
func SliceUniqMap(s []int) []int {
// @see https://www.reddit.com/r/golang/comments/5ia523/idiomatic_way_to_remove_duplicates_in_a_slice/db6qa2e/
seen := make(map[int]struct{}, len(s))
j := 0
for _, v := range s {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
s[j] = v
j++
}
return s[:j]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment