File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,22 @@ func Dedupe[T comparable](inputSlice []T) (result []T) {
3737 return
3838}
3939
40+ // DedupeFunc removes duplicates from a slice of elements using a key function
41+ // The key function is used to return a key for each element in the slice
42+ // and then the key is used to check for duplicates
43+ func DedupeFunc [T any ](inputSlice []T , keyFunc func (T ) any ) (result []T ) {
44+ seen := make (map [any ]struct {})
45+ for _ , inputValue := range inputSlice {
46+ key := keyFunc (inputValue )
47+ if _ , ok := seen [key ]; ! ok {
48+ seen [key ] = struct {}{}
49+ result = append (result , inputValue )
50+ }
51+ }
52+
53+ return
54+ }
55+
4056// PickRandom item from a slice of elements
4157func PickRandom [T any ](v []T ) T {
4258 return v [rand .Intn (len (v ))]
You can’t perform that action at this time.
0 commit comments