This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique.go
224 lines (201 loc) · 4.46 KB
/
unique.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright [2020] [thinkgos]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package numeric
// UniqueInts takes an input slice of ints and
// returns a new slice of ints without duplicate values.
func UniqueInts(a []int) []int {
l := len(a)
if l <= 1 {
return a
}
m := make(map[int]struct{}, l)
r := make([]int, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueUints takes an input slice of uints and
// returns a new slice of uints without duplicate values.
func UniqueUints(a []uint) []uint {
l := len(a)
if l <= 1 {
return a
}
m := make(map[uint]struct{}, l)
r := make([]uint, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueInt8s takes an input slice of int8s and
// returns a new slice of int8s without duplicate values.
func UniqueInt8s(a []int8) []int8 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[int8]struct{}, l)
r := make([]int8, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueUint8s takes an input slice of uint8s and
// returns a new slice of uint8s without duplicate values.
func UniqueUint8s(a []uint8) []uint8 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[uint8]struct{}, l)
r := make([]uint8, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueInt16s takes an input slice of int16s and
// returns a new slice of int16s without duplicate values.
func UniqueInt16s(a []int16) []int16 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[int16]struct{}, l)
r := make([]int16, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueUint16s takes an input slice of uint16s and
// returns a new slice of uint16s without duplicate values.
func UniqueUint16s(a []uint16) []uint16 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[uint16]struct{}, l)
r := make([]uint16, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueInt32s takes an input slice of int32s and
// returns a new slice of int32s without duplicate values.
func UniqueInt32s(a []int32) []int32 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[int32]struct{}, l)
r := make([]int32, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueUint32s takes an input slice of uint32s and
// returns a new slice of uint32s without duplicate values.
func UniqueUint32s(a []uint32) []uint32 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[uint32]struct{}, l)
r := make([]uint32, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueInt64s takes an input slice of int64s and
// returns a new slice of int64s without duplicate values.
func UniqueInt64s(a []int64) []int64 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[int64]struct{}, l)
r := make([]int64, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueUint64s takes an input slice of uint64s and
// returns a new slice of uint64s without duplicate values.
func UniqueUint64s(a []uint64) []uint64 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[uint64]struct{}, l)
r := make([]uint64, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}
// UniqueFloat64s takes an input slice of float64s and
// returns a new slice of float64s without duplicate values.
func UniqueFloat64s(a []float64) []float64 {
l := len(a)
if l <= 1 {
return a
}
m := make(map[float64]struct{}, l)
r := make([]float64, 0, l)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = struct{}{}
r = append(r, v)
}
}
return r
}