We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 05054c6 commit 1d4a72fCopy full SHA for 1d4a72f
coderd/util/slice/slice.go
@@ -38,17 +38,19 @@ func Overlap[T comparable](a []T, b []T) bool {
38
}
39
40
// Unique returns a new slice with all duplicate elements removed.
41
-// This is a slow function on large lists.
42
-// TODO: Sort elements and implement a faster search algorithm if we
43
-// really start to use this.
44
func Unique[T comparable](a []T) []T {
45
cpy := make([]T, 0, len(a))
+ seen := make(map[T]struct{}, len(a))
+
46
for _, v := range a {
47
- v := v
48
- if !Contains(cpy, v) {
49
- cpy = append(cpy, v)
+ if _, ok := seen[v]; ok {
+ continue
50
+ seen[v] = struct{}{}
51
+ cpy = append(cpy, v)
52
53
54
return cpy
55
56
0 commit comments