forked from sneak/util
DedupeStrings returns a new slice holding each value once, in the order each value first appeared, and leaves the input slice alone. Comes with a doc comment and table-driven tests. (closes #1) Model: opus-5
24 lines
516 B
Go
24 lines
516 B
Go
package util
|
|
|
|
// DedupeStrings returns a new slice holding each value of input once, in the
|
|
// order in which each value first appears. The input slice is not modified.
|
|
// A nil input returns nil.
|
|
func DedupeStrings(input []string) []string {
|
|
if input == nil {
|
|
return nil
|
|
}
|
|
|
|
seen := make(map[string]struct{}, len(input))
|
|
out := make([]string, 0, len(input))
|
|
|
|
for _, value := range input {
|
|
if _, ok := seen[value]; ok {
|
|
continue
|
|
}
|
|
seen[value] = struct{}{}
|
|
out = append(out, value)
|
|
}
|
|
|
|
return out
|
|
}
|