MCPcopy Create free account
hub / github.com/adam-hanna/arrayOperations / Distinct

Function Distinct

arrayOperations.go:90–109  ·  view source on GitHub ↗

Distinct returns the unique vals of a slice fmt.Println(Distinct[int]([]int{1, 1, 2, 3})) // output: [1, 2, 3]

(arrs ...[]T)

Source from the content-addressed store, hash-verified

88// fmt.Println(Distinct[int]([]int{1, 1, 2, 3}))
89// // output: [1, 2, 3]
90func Distinct[T comparable](arrs ...[]T) []T {
91 // put the values of our slice into a map
92 // the key's of the map will be the slice's unique values
93 m := make(map[T]struct{})
94 for idx1 := range arrs {
95 for idx2 := range arrs[idx1] {
96 m[arrs[idx1][idx2]] = struct{}{}
97 }
98 }
99
100 // create the output slice and populate it with the map's keys
101 res := make([]T, len(m))
102 i := 0
103 for k := range m {
104 res[i] = k
105 i++
106 }
107
108 return res
109}
110
111// Intersect returns a slice of values that are present in all of the input slices
112//

Callers 4

TestDistinctFunction · 0.85
IntersectFunction · 0.85
UnionFunction · 0.85
DifferenceFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestDistinctFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…