underscore/unique_in_place.go
Ruidy 9cf61ec6c5
feat: add ParallelFilter and UniqueInPlace functions
Add `ParallelFilter` for concurrent filtering with context and error
support.
Add `UniqueInPlace` to remove duplicates from slices in place. Update
README
and add documentation and tests for both functions.
2025-09-01 18:16:59 -04:00

17 lines
431 B
Go

package underscore
// UniqueInPlace removes duplicate elements from the slice in place, preserving order.
// It returns the shortened slice containing the first occurrence of each value.
func UniqueInPlace[T comparable](values []T) []T {
seen := make(map[T]struct{}, len(values))
w := 0
for _, v := range values {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
values[w] = v
w++
}
return values[:w]
}