-
Notifications
You must be signed in to change notification settings - Fork 2
/
unique.js
345 lines (324 loc) · 8.31 KB
/
unique.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Copyright © https://github.com/jarry All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
*/
// the multi method for array unique
// JavaScript数组去重的N种方法之新数组检测法
(function () {
// 1. 新建数组,把不存在的项添加进去
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
const newArr = []
// 遍历数组,逐项比较添加到新数组中去
for (let i = 0, l = arr.length; i < l; i++) {
for (let j = 0; j <= i; j++) {
// 检查新数组是否已存在要添加的项
if (arr[i] === arr[j]) {
if (i === j) {
newArr.push(arr[i])
}
// 如果新数组中已存在要添加项则跳过
break
}
}
}
console.log('new array result:', newArr)
console.timeEnd('time')
})();
(function () {
// 1.1 新建数组与indexOf检查是否存在
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
const newArr = []
for (let i = 0, l = arr.length; i < l; i++) {
if (newArr.indexOf(arr[i]) < 0) {
newArr.push(arr[i])
}
}
console.log('new array + indexOf:', newArr)
console.timeEnd('time')
})();
(function () {
// 1.2 新建数组与includes检查是否包含
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
const newArr = []
for (let i = 0, l = arr.length; i < l; i++) {
if (!newArr.includes(arr[i])) {
newArr.push(arr[i])
}
}
console.log('new array + includes:', newArr)
console.timeEnd('time')
})();
(function () {
// 2.1 从后往前逐个对比,将重复的删除掉
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
let len = arr.length
while (len-- > 0) {
for (let i = 0; i < len; i++) {
if (arr[len] === arr[i]) {
arr.splice(len, 1)
break
}
}
}
console.log('one array last -> first result:', arr)
console.timeEnd('time')
})();
(function () {
// 2.2 从后往前逐个对比,将重复的删除掉
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
let len = arr.length
let i = 0
while (len-- > 0) {
i = len
while (i-- > 0) {
if (arr[len] === arr[i]) {
arr.splice(len, 1)
break
}
}
}
console.log('one array last -> first from right:', arr)
console.timeEnd('time')
})();
(function () {
// 3. 从前往后逐个对比,将重复的删除掉
console.time('time')
const arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
let len = arr.length
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1)
len--
i--
break
}
}
}
console.log('one array first -> last result:', arr)
console.timeEnd('time')
})();
(function () {
// 4. forEach + indexOf
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var newArr = []
for (var i = 0, l = arr.length; i < l; i++) {
if (i === arr.indexOf(arr[i])) {
newArr.push(arr[i])
} else if (arr.indexOf(arr[i]) >= 0) {
continue
}
}
console.log('forEach + indexOf result:', newArr)
console.timeEnd('time')
})();
(function () {
// 5. filter + indexOf
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
arr = arr.filter((item, i) => i === arr.indexOf(item))
console.log('filter + indexOf: ', arr)
console.timeEnd('time')
})();
(function () {
// 6. Array.from object
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var obj = {}
arr.forEach((item) => {
obj[item] = item
})
console.log('Array.from object:', Array.from(Object.values(obj)))
console.timeEnd('time')
})();
(function () {
// 7. Array.from Map
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var map = new Map()
arr.forEach((item, i) => {
map.set(item, item)
})
console.log('Array.from Map:', Array.from(map.keys()))
console.timeEnd('time')
})();
(function () {
// 8. from Set
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var set = new Set(arr)
console.log('from Set:', Array.from(set))
console.timeEnd('time')
})();
(function () {
// 9. sort+remove
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
arr.sort()
var l = arr.length
while (l-- > 1) {
if (arr[l] === arr[l - 1]) {
arr.splice(l, 1)
}
}
console.log('sort+remove:', arr)
console.timeEnd('time')
})();
(function () {
// 10. sort+remove ASE
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
arr.sort((a, b) => {
return a.toString() > b.toString() ? 1 : -1
})
var l = arr.length
for (var i = 0; i < l - 1; i++) {
if (arr[i] === arr[i + 1]) {
arr.splice(i, 1)
l--
i--
}
}
console.log('sort+remove ASE:', arr)
console.timeEnd('time')
})();
(function () {
// 11. from reduce
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var newArr = arr.reduce((result, item) => {
if (!Array.isArray(result)) {
result = [result]
}
return result.includes(item) ? result : [...result].concat(item)
})
console.log('from reduce:', newArr)
console.timeEnd('time')
})();
(function () {
// 12. indexOf + new Array push
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var newArr = []
for (var i = 0, l = arr.length; i < l; i++) {
var item = arr[i]
if (newArr.indexOf(item) < 0) {
newArr.push(item)
}
}
console.log('new Array push:', newArr)
console.timeEnd('time')
})();
(function () {
// 13. filter+hasOwnProperty
console.time('time')
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
var obj = {}
arr = arr.filter(item => {
return obj.hasOwnProperty((typeof item) + item) ? false : (obj[(typeof item) + item] = true)
})
console.log('filter+hasOwnProperty:', arr)
console.timeEnd('time')
})();
(function () {
// 14. recursion
console.time('time')
function recursionUnique(arr, len) {
if (len <= 1) {
return arr
}
var l = len
var last = l - 1
var isRepeat = false
while (l-- > 1) {
if (arr[last] === arr[l - 1]) {
isRepeat = true
break
}
}
if (isRepeat) {
arr.splice(last, 1)
}
return recursionUnique(arr, len - 1)
}
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
console.log('recursionUnique:', recursionUnique(arr, arr.length))
arr = [1, 3, -1, 1, 2, 2, 4, 2, 2, -1]
console.log('recursionUnique:', recursionUnique(arr, arr.length))
console.timeEnd('time')
})();
(function () {
// 15. recursionUniqueNew
console.time('time')
function recursionUniqueNew(arr, len) {
if (len < 1) {
return []
}
var l = len
var last = l - 1
var lastItem = arr[last]
var isRepeat = false
var result = []
while (l-- > 1) {
if (lastItem === arr[l - 1]) {
isRepeat = true
break
}
}
if (!isRepeat) {
result.push(lastItem)
}
return recursionUniqueNew(arr, len - 1).concat(result)
}
var arr = ['a', 'a', 1, 1, 2, 2, 'b', 'b', 2, '1']
console.log('recursionUniqueNew:', recursionUniqueNew(arr, arr.length))
arr = [1, 3, -1, 1, 2, 2, 4, 2, 2, -1]
console.log('recursionUniqueNew:', recursionUniqueNew(arr, arr.length))
console.timeEnd('time')
})()
/**
jarrys-MacBook-Pro:unique jarry$ node unique.js
new array result: [ 'a', 1, 2, 'b' ]
time: 4.023ms
new array + indexOf: [ 'a', 1, 2, 'b' ]
time: 0.215ms
new array + includes: [ 'a', 1, 2, 'b' ]
time: 0.208ms
one array last -> first result: [ 'a', 1, 2, 'b' ]
time: 0.057ms
one array last -> first from right: [ 'a', 1, 2, 'b' ]
time: 0.046ms
one array first -> last result: [ 'a', 1, 2, 'b' ]
time: 0.046ms
forEach + indexOf result: [ 'a', 1, 2, 'b' ]
time: 0.052ms
filter + indexOf: [ 'a', 1, 2, 'b' ]
time: 0.080ms
Array.from object: [ 1, 2, 'a', 'b' ]
time: 0.276ms
Array.from Map: [ 'a', 1, 2, 'b' ]
time: 0.382ms
from Set: [ 'a', 1, 2, 'b' ]
time: 0.348ms
sort+remove: [ 1, 2, 'a', 'b' ]
time: 0.091ms
sort+remove ASE: [ 1, 2, 'a', 'b' ]
time: 0.298ms
from reduce: [ 'a', 1, 2, 'b' ]
time: 0.426ms
new Array push: [ 'a', 1, 2, 'b' ]
time: 0.068ms
filter+hasOwnProperty: [ 'a', 1, 2, 'b' ]
time: 0.271ms
recursionUnique: [ 'a', 1, 2, 'b' ]
time: 0.119ms
recursionUniqueNew: [ 'a', 1, 2, 'b' ]
time: 0.110ms
*/