MCPcopy Create free account
hub / github.com/github/gh-aw / Deduplicate

Function Deduplicate

pkg/sliceutil/sliceutil.go:77–90  ·  view source on GitHub ↗

Deduplicate returns a new slice with duplicate elements removed. The order of first occurrence is preserved. This is a pure function that does not modify the input slice.

(slice []T)

Source from the content-addressed store, hash-verified

75// The order of first occurrence is preserved.
76// This is a pure function that does not modify the input slice.
77func Deduplicate[T comparable](slice []T) []T {
78 seen := make(map[T]struct{}, len(slice))
79 result := make([]T, 0, len(slice))
80 for _, item := range slice {
81 if _, ok := seen[item]; !ok {
82 seen[item] = struct{}{}
83 result = append(result, item)
84 }
85 }
86 if sliceutilLog.Enabled() && len(result) < len(slice) {
87 sliceutilLog.Printf("Deduplicate: removed %d duplicate(s) from %d items", len(slice)-len(result), len(slice))
88 }
89 return result
90}
91
92// MergeUnique returns a deduplicated slice that starts with base and appends any
93// items from extra that are not already present in base. Order is preserved.

Calls 2

EnabledMethod · 0.45
PrintfMethod · 0.45

Tested by 3

TestDeduplicateFunction · 0.68