Skip to content

Instantly share code, notes, and snippets.

@rbrick
Created June 19, 2016 06:14
Show Gist options
  • Save rbrick/fc6d9837a7e3d470a94ae0a5a8a58c63 to your computer and use it in GitHub Desktop.
Save rbrick/fc6d9837a7e3d470a94ae0a5a8a58c63 to your computer and use it in GitHub Desktop.
Merges multiple slices into one without duplicates.
// Merges slices into one without duplicates
func merge(slices... []string) []string {
merged := *new([]string)
// Use a map for faster lookup
contains := map[string]bool{}
for _, v := range slices {
for _, s := range v {
if _, ok := contains[s]; ok {
// already contains. Skip!
continue
}
// append.
merged = append(merged, s)
contains[strings.ToLower(s)] = true
}
}
return merged
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment