Union returns a slice that contains the unique values of all the input slices example #1 a := []int{1, 2, 2, 4, 6} b := []int{2, 4, 5} fmt.Println(Union[int](a, b)) output: []int{1, 2, 4, 5, 6} example #2 fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6})) output: []int{1, 3, 4, 5, 6}
(arrs ...[]T)
| 165 | // fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6})) |
| 166 | // // output: []int{1, 3, 4, 5, 6} |
| 167 | func Union[T comparable](arrs ...[]T) []T { |
| 168 | m := make(map[T]struct{}) |
| 169 | |
| 170 | var tmpArr []T |
| 171 | for idx := range arrs { |
| 172 | tmpArr = Distinct(arrs[idx]) |
| 173 | |
| 174 | for idx2 := range tmpArr { |
| 175 | m[tmpArr[idx2]] = struct{}{} |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ret := make([]T, len(m)) |
| 180 | i := 0 |
| 181 | for k := range m { |
| 182 | ret[i] = k |
| 183 | i++ |
| 184 | } |
| 185 | |
| 186 | return ret |
| 187 | } |
| 188 | |
| 189 | // Difference returns a slice of values that are only present in one of the input slices |
| 190 | // |