Skip to content

Commit

Permalink
refactor slice.Unique() (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-liyang authored May 9, 2024
1 parent de9ee08 commit 53fa210
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,21 +772,14 @@ func UpdateAt[T any](slice []T, index int, value T) []T {
// Play: https://go.dev/play/p/AXw0R3ZTE6a
func Unique[T comparable](slice []T) []T {
result := []T{}

for i := 0; i < len(slice); i++ {
v := slice[i]
skip := true
for j := range result {
if v == result[j] {
skip = false
break
}
}
if skip {
result = append(result, v)
exists := map[T]bool{}
for _, t := range slice {
if exists[t] {
continue
}
exists[t] = true
result = append(result, t)
}

return result
}

Expand Down

0 comments on commit 53fa210

Please sign in to comment.