Javascript array de-duplication method aggregation _ javascript skills

Source: Internet
Author: User
Array deduplication is a common requirement. we will temporarily consider repeated arrays of the same type. It mainly aims to clarify the ideas and consider the performance. The following methods are available on the internet. here is a brief summary of the methods. Four algorithms to achieve this goal:

First:

Array. prototype. unique1 = function () {var n = []; // A new temporary array for (var I = 0; I <this. length; I ++) // traverse the current array {// skip this step if the I of the current array has been saved as a temporary array, // otherwise, push the current item to the temporary array if (n. indexOf (this [I]) =-1) n. push (this [I]);} return n ;}

Second:

Array. prototype. unique2 = function () {var n = {}, r = []; // n is a hash table, and r is a temporary array for (var I = 0; I <this. length; I ++) // traverses the current array {if (! N [this [I]) // if the hash table does not have the current item {n [this [I] = true; // save it to the 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 I cannot. I have considered the "222" and "222" problems.

Version 2:

// The Digest version of the hash method. 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] = true; r. push (this [I])} return r}; var arr = ["222", 222,2,]; var newarry = arr. unique2 (); console. log (newarry [newarry. length-1]);

Third:

Array. prototype. unique3 = function () {var n = [this [0]; // result array for (var I = 1; I <this. length; I ++) // traverse from the second entry {// if the position where the I entry of the current array appears for the first time in the current array is not I, // This indicates that the I-th item is repeated and ignored. Otherwise, store the result array if (this. indexOf (this [I]) = I) n. push (this [I]) ;}return n ;}

The indexOf method of the array is used in both the 1st and 3rd methods. The purpose of this method is to find the location where the saved parameter appears for the first time in the array. Obviously, the js engine will traverse the array when implementing this method until it finds the target. Therefore, this function will waste a lot of time. The method in 2nd uses the hash table. Store existing objects in the form of a subobject. The reference of the underlying object is much faster than the search for an array using indexOf.

To determine the efficiency of these three methods, I made a test program to generate an array consisting of a random number of 10000 characters and then used several methods to test the execution time. The result shows that the second method is much faster than the other two methods. However, there should be more than one method for memory usage because one hash table is added. This is the so-called space change time. This is the test page. you can also check it.

Method 4:

Array.prototype.unique4 = function(){    this.sort();    var re=[this[0]];    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 first sort the array and then compare the adjacent two values. The JS native sort method is used for sorting. the JS engine should use quick sorting internally. The final test result is that the average running time of this method is about three times that of the second method, but it is much faster than the first and third methods.

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.