Distinct returns the unique vals of a slice fmt.Println(Distinct[int]([]int{1, 1, 2, 3})) // output: [1, 2, 3]
(arrs ...[]T)
| 88 | // fmt.Println(Distinct[int]([]int{1, 1, 2, 3})) |
| 89 | // // output: [1, 2, 3] |
| 90 | func 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 | // |
no outgoing calls
searching dependent graphs…