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)
| 75 | // The order of first occurrence is preserved. |
| 76 | // This is a pure function that does not modify the input slice. |
| 77 | func 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. |