1
0
forked from sneak/util
Files
util/dedupe.go
clawbot 7fb688772e add DedupeStrings for removing repeated values from a string slice
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
2026-09-05 03:23:28 +00:00

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
}