Created
June 19, 2016 06:14
-
-
Save rbrick/fc6d9837a7e3d470a94ae0a5a8a58c63 to your computer and use it in GitHub Desktop.
Merges multiple slices into one without duplicates.
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
// 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