-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUniqueArray.js
329 lines (328 loc) · 9.22 KB
/
UniqueArray.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
/**
* Copyright © https://github.com/jarry All rights reserved.
* @author: [email protected]
* @version: 1.0
*/
// the multi method for array unique
// JavaScript数组去重的N种方法
class UniqueArray {
constructor(arr) {
this.arr = arr;
}
// 1. new array
unique1() {
console.time("time");
var arr = this.arr.slice(0);
var newArr = [];
for (var i = 0, l = arr.length; i < l; i++) {
for (var 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");
}
unique2() {
// 2. new array + indexOf
console.time("time");
var arr = this.arr.slice(0);
var newArr = [];
for (var 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");
}
unique3() {
// 3. new array + includes
console.time("time");
var arr = this.arr.slice(0);
var newArr = [];
for (var 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");
}
unique4() {
// 4. one array last -> first
console.time("time");
var arr = this.arr.slice(0);
var l = arr.length;
while (l-- > 0) {
for (var i = 0; i < l; i++) {
if (arr[l] === arr[i]) {
arr.splice(l, 1);
break;
}
}
}
console.log("one array last -> first result:", arr);
console.timeEnd("time");
}
unique5() {
// 5. one array last -> first from right
console.time("time");
var arr = this.arr.slice(0);
var l = arr.length;
var i = 0;
while (l-- > 0) {
i = l;
while (i-- > 0) {
if (arr[l] === arr[i]) {
arr.splice(l, 1);
break;
}
}
}
console.log("one array last -> first from right:", arr);
console.timeEnd("time");
}
unique6() {
// 6. one array first -> last
console.time("time");
var arr = this.arr.slice(0);
var l = arr.length;
for (var i = 0; i < l; i++) {
for (var j = i + 1; j < l; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1);
l--;
i--;
break;
}
}
}
console.log("one array first -> last result:", arr);
console.timeEnd("time");
}
unique7() {
// 7. forEach + indexOf
console.time("time");
var arr = this.arr.slice(0);
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) {
// break same item
continue;
}
}
console.log("forEach + indexOf result:", newArr);
console.timeEnd("time");
}
unique8() {
// 8. filter + indexOf
console.time("time");
var arr = this.arr.slice(0);
console.log("filter + indexOf: ", arr.filter((item, i) => i === arr.indexOf(item)));
console.timeEnd("time");
}
unique9() {
// 9. Array.from object
console.time("time");
var arr = this.arr.slice(0);
var obj = {};
arr.forEach((item) => {
obj[item] = item;
});
console.log("Array.from object:", Array.from(Object.values(obj)));
console.timeEnd("time");
}
unique10() {
// 10. Array.from Map
console.time("time");
var arr = this.arr.slice(0);
var map = new Map();
arr.forEach((item, i) => {
map.set(item, item);
});
console.log("Array.from Map:", Array.from(map.values()));
console.timeEnd("time");
}
unique11() {
// 11. from Set
console.time("time");
var arr = this.arr.slice(0);
var set = new Set(arr);
console.log("from Set:", Array.from(set));
console.timeEnd("time");
}
unique12() {
// 12. sort+remove
console.time("time");
var arr = this.arr.slice(0);
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");
}
unique13() {
// 13. sort+remove ASE
console.time("time");
var arr = this.arr.slice(0);
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");
}
unique14() {
// 14. from reduce
console.time("time");
var arr = this.arr.slice(0);
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");
}
unique15() {
// 15. indexOf + new Array push
console.time("time");
var arr = this.arr.slice(0);
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");
}
unique16() {
// 16. filter+hasOwnProperty
console.time("time");
var arr = this.arr.slice(0);
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");
}
recursionUnique(arr, len) {
// 17. recursion
console.time("time");
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 this.recursionUnique(arr, len - 1);
}
recursionUniqueNew(arr, len) {
// 18. recursionUniqueNew
console.time("time");
if (len <= 1) {
return arr;
}
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;
}
}
arr.splice(last);
if (!isRepeat) {
result.push(lastItem);
}
return this.recursionUniqueNew(arr, len - 1).concat(result);
}
}
(function () {
var arr = [1, 1, 3, -1, 1, 2, 2, 4, 2, 2, -1];
const unique = new UniqueArray(arr);
console.log("begin to test:\n");
for (var i = 1; i <= 16; i++) {
unique["unique" + i]();
}
console.log("recursionUnique:", unique.recursionUnique(arr.slice(0), arr.length));
console.timeEnd("time");
console.log("recursionUniqueNew:", unique.recursionUniqueNew(arr.slice(0), arr.length));
console.timeEnd("time");
})();
/**
begin to test:
new array result: [ 1, 3, -1, 2, 4 ]
time: 2.387ms
new array + indexOf: [ 1, 3, -1, 2, 4 ]
time: 0.197ms
new array + includes: [ 1, 3, -1, 2, 4 ]
time: 0.209ms
one array last -> first result: [ 1, 3, -1, 2, 4 ]
time: 0.064ms
one array last -> first from right: [ 1, 3, -1, 2, 4 ]
time: 0.054ms
one array first -> last result: [ 1, 3, -1, 2, 4 ]
time: 0.052ms
forEach + indexOf result: [ 1, 3, -1, 2, 4 ]
time: 0.059ms
filter + indexOf: [ 1, 3, -1, 2, 4 ]
time: 0.076ms
Array.from object: [ 1, 2, 3, 4, -1 ]
time: 0.278ms
Array.from Map: [ 1, 3, -1, 2, 4 ]
time: 0.302ms
from Set: [ 1, 3, -1, 2, 4 ]
time: 0.274ms
sort+remove: [ -1, 1, 2, 3, 4 ]
time: 0.552ms
sort+remove ASE: [ -1, 1, 2, 3, 4 ]
time: 0.114ms
from reduce: [ 1, 3, -1, 2, 4 ]
time: 0.429ms
new Array push: [ 1, 3, -1, 2, 4 ]
time: 0.078ms
filter+hasOwnProperty: [ 1, 3, -1, 2, 4 ]
time: 0.282ms
recursionUnique: [ 1, 3, -1, 2, 4 ]
time: 0.049ms
recursionUniqueNew: [ 1, 3, -1, 2, 4 ]
time: 0.051ms
*/