Skip to content

Commit

Permalink
Uniq(): preserve order
Browse files Browse the repository at this point in the history
  • Loading branch information
aine-etke committed Oct 22, 2024
1 parent 1198db6 commit 513dcab
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import "cmp"
// Uniq removes duplicates from slice
func Uniq(slice []string) []string {
uniq := map[string]struct{}{}
result := []string{}
for _, k := range slice {
uniq[k] = struct{}{}
if _, ok := uniq[k]; !ok {
uniq[k] = struct{}{}
result = append(result, k)
}
}

return MapKeys(uniq)
return result
}

// MergeSlices and remove duplicates
Expand Down

0 comments on commit 513dcab

Please sign in to comment.