mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 10:12:29 +08:00
refactor slice.Unique() (#215)
This commit is contained in:
@@ -772,21 +772,14 @@ func UpdateAt[T any](slice []T, index int, value T) []T {
|
|||||||
// Play: https://go.dev/play/p/AXw0R3ZTE6a
|
// Play: https://go.dev/play/p/AXw0R3ZTE6a
|
||||||
func Unique[T comparable](slice []T) []T {
|
func Unique[T comparable](slice []T) []T {
|
||||||
result := []T{}
|
result := []T{}
|
||||||
|
exists := map[T]bool{}
|
||||||
for i := 0; i < len(slice); i++ {
|
for _, t := range slice {
|
||||||
v := slice[i]
|
if exists[t] {
|
||||||
skip := true
|
continue
|
||||||
for j := range result {
|
|
||||||
if v == result[j] {
|
|
||||||
skip = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if skip {
|
|
||||||
result = append(result, v)
|
|
||||||
}
|
}
|
||||||
|
exists[t] = true
|
||||||
|
result = append(result, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user