mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
20 lines
424 B
Go
20 lines
424 B
Go
package utils
|
|
|
|
// Unique returns a new slice containing only the unique elements of the input slice,
|
|
// preserving their original order.
|
|
func Unique[T comparable](slice []T) []T {
|
|
if slice == nil {
|
|
return nil
|
|
}
|
|
seen := make(map[T]struct{})
|
|
result := make([]T, 0)
|
|
for _, item := range slice {
|
|
if _, ok := seen[item]; ok {
|
|
continue
|
|
}
|
|
seen[item] = struct{}{}
|
|
result = append(result, item)
|
|
}
|
|
return result
|
|
}
|