JavaScript array _javascript method Rollup techniques

Source: Internet
Author: User
Tags javascript array

Four algorithms to achieve this goal:

First type:

Array.prototype.unique1 = function () {
var n = [];//A new temporary array for
(var i = 0; i < this.length; i++)//traverse the current array
{
//If Part I of the current array has been saved in a temporary array, skip,
//or push the current item to the temporary array if
(N.indexof (this[i) = = 1) n.push (this[i));
} return
N;
}

The second type:

Array.prototype.unique2 = function ()
{
var n = {},r=[];//n is a hash table, r is a temporary array for
(var i = 0; i < this.length; i++)//traverse the current array
{if
(!n[this[i]])//If there is no current item
{N[this[i] in the hash table
= true;//deposit Hash Table
R.push (this [i]); Push the current item of the current array to the temporary array} return
r;/
/This method is recommended, but it is not considered "222" and 222 of the problem

The second version of the improvement:

Improved version of
Array.prototype.unique2 = function () {
var n = {}, r = [];
for (var i = 0; i < this.length i++) {
if (!n[typeof (this[i) + this[i]]) {
n[typeof (this[i]) + this[i] = t Rue;
R.push (This[i])
}} return
r
};

var arr=["222", 222,2,2,3];
var newarry=arr.unique2 ();
Console.log (Newarry[newarry.length-1]);

The third type:

Array.prototype.unique3 = function ()
{
var n = [this];//result array for
(var i = 1; i < this.length; i++)// Traversal from the second item
{
//If the first occurrence of the current array in the current array is not the position I,
//Then the item I is duplicated, ignored. Otherwise deposit the result array
if (This.indexof (this[i]) = = i) N.push (This[i]);
}
return n;
}

Both the 1th and 3rd methods use the IndexOf method of the array. The purpose of this method is to find the position where the parameter is stored for the first time in the array. It is obvious that the JS engine will iterate through the array when it implements this method until it finds the target. So this function will waste a lot of time. The 2nd method uses a hash table. To deposit an object in a form that has already appeared through the subscript. The subscript reference is much faster than searching the array with indexof.

To determine the efficiency of these three methods, I did a test program that generated an array of 10000-length random numbers, and then tested the execution time in several ways. The results show that the second method is much faster than the other two methods. But memory footprint should be more than the second method, because there is more than one hash table.  This is called space exchange time. This is the test page, you can also go to see.

The fourth method:

Array.prototype.unique4 = function ()
{
this.sort ();
var re=[this];
for (var i = 1; i < this.length i++)
{
if (This[i]!== re[re.length-1])
{
Re.push (this[i));
}
return re;
}

The idea of this method is to sort the array first and then compare the adjacent two values. The sort time uses the JS native sort method, the JS engine interior should be uses the quick sort. The result of the final test is that this method runs about three times times the average of the second method, but much faster than the first and third methods.

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD