Realization of array duplication removal method and efficiency ratio by js
- 2021-07-21 06:50:36
- OfStack
The reason why these methods are searched in piles on the reality sheet is mainly because I forgot how other methods are practical after learning 1, so I made a try to see which one is the most efficient. For better display effect, I will first make a try to sum up and sum up the method that is better than others. (Reminder: The following is just a tussle, and it is easy to pollute the overall situation if you don't build a tussle on the prototype under the same situation.)
1. Find the correspondence through the past
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique1());
2. Find a number of positions through the past
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique2());
3. It is similar to looking for the position of the number. Is the position of searching for the first appearance of the number word different from the previous position
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique3());
4. Compare with each other, sort first, and then compare with each other. Are the two numbers equal
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
console.log(n.unique4());
Next, there is a less efficiency ratio, which is also the emphasis of this article. We have a number of time functions for 1 time ratio.
First, generate the number of 1 hundred words
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log(' This letter has cost me: ' + (end - start) + ' Milliseconds ');
}
var ary = randomAry(100),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
The knot is made in my Google scout
Method 1: 0 milliseconds
Method 2: 1 millisecond
Method 3: 0 milliseconds
Method 4:00 milliseconds
(Well, sure enough, the current generation of scramblers is incomparable, and they don't spit out the old scramblers.)
Look at 1000 words
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log(' This letter has cost me: ' + (end - start) + ' Milliseconds ');
}
var ary = randomAry(1000),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
Method 1 : 1-2 Milliseconds
Method 2 : 40-50 Milliseconds
Method 3 : 40-50 Milliseconds
Method 4 : 0-1 Milliseconds
Take a look 10000 Counting words (the number is as big as it is, it can't stand it at first, and so on 56 Seconds)
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log(' This letter has cost me: ' + (end - start) + ' Milliseconds ');
}
var ary = randomAry(10000),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
Because the number is so big, I take the number once, and how many times will you refresh the comparison when you are happy
Method 1:10 milliseconds
Method 2: 1258 ms
Method 3: 2972 milliseconds
Method 4: 5 milliseconds
Originally, I wanted to give a bigger reference, but then 100,000 of my pages ran away. . . It's over. . . It's over
After that, I tried 50,000 people and ran away. . . It's over. . . It's over
Then forget it,
Anyway, in 10,000 numbers, the larger the numbers, the increase in milliseconds between Method 1 and Method 4, and the efficiency of Method 2 and Method 3 is touching
Efficiency calculation: 4 > 1 > 2 > 3
Method calculation: Sort first, and then compare whether the first two numbers are equal > Looking for the stability of the image through the past > Go through and find a number of locations > Is the position of the first appearance of the search numeral word 1 different from the previous position