mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
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.
17 lines
431 B
Go
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]
|
|
}
|