Array de-weight javascript

Source: Internet
Author: User

Array removal of duplicate values is the interview often test content, is also very easy to use a technique, the following several ways to introduce the weight.

The first is the most common method, which is to add a temporary array, the original array traversal, join the temporary array, each time you join to determine whether the element to be added in the temporary array, the code is as follows:

To redo an array, only consider the elements in the arrays as numbers or strings, return a de-heavy array//First method, Traverse, will not insert temporary array function uniqArray1 (arr) {    var n=[];    for (Var i=0;i<arr.length;i++) {    //If the current item has been saved to a temporary array, skip, otherwise join    if (N.indexof (Arr[i]) ==-1) {    N.push (arr[ I]);    }    }    return n;}

So is there a better way? It is possible to adopt the idea of a hash table, in JavaScript, where objects are searched much faster than array subscripts. So we can create an object specifically to hold elements that have been added to the temporary array, so that each time a new element is added, it can be used to find out if the object is duplicated, and the code is as follows:

The second method uses the hash table function UniqArray2 (arr) {var n={},//hash table r=[];//-zero array for (Var i=0;i<arr.length;i++) {if (n[arr[i]]== NULL) {//If not in the hash table, add to the Hashtable and enter the temporary array N[arr[i]]=true;r.push (Arr[i]);}} return r;}

There is also a way, although the speed of the hash table is fast, but more than the basic indexof come fast, the idea is to go through the sorting function sort, and then compare the adjacent elements, the difference is added to the temporary array. The code is as follows:

The third method, sorted first, then compares the adjacency part function uniqArray3 (arr) {arr.sort (); var r=[arr[0]];for (var i=1;i<arr.length;i++) {if (arr[i]! =r[r.length-1]) {//because it has been sorted, so the adjacent is the same r.push (Arr[i]);}} return r;}

The final experiment code is as follows:

To verify the use of the array de-weight function var arr=[2,3,4,2,4,5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3];for (var i=0;i<10000000 i++) {//In order to discern the difference of time spent, we hereby increase the length of the array Arr.push (3);} var time1=date.now (), Var n1=uniqarray1 (arr), Var time2=date.now (), Console.log (N1),//2,3,4,5 illustrates the first kind of de-re-success Console.log ( TIME2-TIME1);//218time1=date.now (); var n2=uniqarray2 (arr); Time2=date.now (); Console.log (n2);// 2,3,4,5 illustrates the second kind of de-console.log (TIME2-TIME1);//63, indicating that the reference to the object subscript is much faster than the indexof search time1=date.now (); var n3=uniqarray3 (arr); Time2=date.now (); Console.log (n3);//2,3,4,5 illustrates the third kind of de-success console.log (TIME2-TIME1);//203, which shows that the sort method uses a quick row, faster than IndexOf, But no hash is fast.

You can see the speed of the hashing algorithm is the fastest.

The full code location is as follows: Http://runjs.cn/code/feqvgket


Array de-weight javascript

Related Article

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.