Added 'unique append' function, to ensure that elements in slice are unique

master
Aadhavan Srinivasan 2 months ago
parent b1e2d7147e
commit 50e86b5db4

@ -1,6 +1,7 @@
package main package main
import ( import (
"slices"
"unicode" "unicode"
) )
@ -13,3 +14,14 @@ func assert(cond bool) {
panic("Assertion Failed") panic("Assertion Failed")
} }
} }
// Ensure that the given elements are only appended to the given slice if they
// don't already exist.
func unique_append[T comparable](slc []T, items ...T) []T {
for _, item := range items {
if !slices.Contains(slc, item) {
slc = append(slc, item)
}
}
return slc
}

Loading…
Cancel
Save