From 50e86b5db42015601c9325dfb9a2ca0be5b90686 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 27 Oct 2024 12:52:35 -0400 Subject: [PATCH] Added 'unique append' function, to ensure that elements in slice are unique --- misc.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/misc.go b/misc.go index 0674718..5b89537 100644 --- a/misc.go +++ b/misc.go @@ -1,6 +1,7 @@ package main import ( + "slices" "unicode" ) @@ -13,3 +14,14 @@ func assert(cond bool) { 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 +}