1
0
forked from sneak/util

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
This commit is contained in:
2026-09-05 03:23:28 +00:00
parent 9302c14a6c
commit 7fb688772e
2 changed files with 63 additions and 0 deletions

23
dedupe.go Normal file
View File

@@ -0,0 +1,23 @@
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
}