JS to remove the array duplicates

Source: Internet
Author: User
Tags hasownproperty

The first write, feel do not know what to write, think about a half-day decision or use some of the previously used things, read to write the array to remove duplicates forget ^_^.

Remove array Duplicates There are many ways, before because of the use, looked at the previous collection of some code, to compare, summed up here, I hope to give you some help, what the problem, please the Great God to guide you.

method One:

//Two for LoopsARRAY.PROTOTYPE.DELREPEAT1 =function () {        varR =NewArray (); Label: for(vari = 0, n = This. length; I < n; i++) {             for(varx = 0, y = r.length; x < y; X + +) {                if(R[x] = = This[i]) {Continuelabel; }} R[r.length]= This[i]; }        returnR; }

This is my first exposure to the weight, I believe many people are also the first contact with this method, this method is used to run the cycle too much, inefficient, if the amount of data is still OK, if it is a large number of data, then can only say hehe.

Method Two:

 Array.prototype.delRepeat2 = function   () {  this . Sort (); //         sort  var  n = [this  [0         for  (var  i = 1; i < this . Length; I++ this  [i]!== n[n.length-1]) {N.push ( this  [i]);    }}  return   n; }

This is what I prefer to use, the efficiency is good, first with sort, add a temporary array, and then loop through, the de-heavy array and the temporary array at the end of the comparison, the difference is to join the temporary array.

Method Three:

 ARRAY.PROTOTYPE.DELREPEAT3 = function   () {  var  n = []; //         a new temporary array  for  (var  i = 0; i < //  traverse the current array   { //   temporary array     There, skipped,   Otherwise push the current item to the temporary array  
            if (N.indexof (This [i]) = =-1) n.push(This[i])        ;        } return n;    }

Method Three Although there is only one for loop, but the IndexOf function retrieves n arrays from the beginning, the same efficiency is not.

Method Four:

ARRAY.PROTOTYPE.DELREPEAT4 =function () {        varn = {}, r = [];//n is a hash table and r is a temporary array         for(vari = 0; I < This. length; i++)//iterate through the current array        {            if(!n[ This[i]])//If there is no current item in the hash table{n[ This[i]] =true;//Deposit Hash TableR.push ( This[i]);//Push the current item of the current array into the temporary array            }        }        returnR; }

This method is very efficient, but there is a big drawback, that is, memory consumption. It is not recommended.

Method Five:

ARRAY.PROTOTYPE.DELREPEAT5 =function () {        varn = [ This[0]];//result Array         for(vari = 1; I < This. length; i++)//start traversing from the second item        {            //if the item I of the current array first appears in the current array, the position is not I,            //then it means that item I is repeated and ignored. Otherwise, the result array is stored            if( This. IndexOf ( This[i]) = = i) N.push ( This[i]); }        returnN; }

This method and method used the same indexof.

Method Six:

function () {        returnthis. Sort (). Join (",,"). Replace (/(, |^) ([^,]+) (,, \2) + (, |$)/g, "$1$2$4"). Replace (/,,+/g, ","). Replace (/,$/, ""). Split (",");    

This needless to say, with the regular, O (╯-╰) O, feeling very complex, I do not understand σ (° °| | |) ︴。

Method Seven:

ARRAY.PROTOTYPE.DELREPEAT7 =function () {        vartemp = {}, Len = This. Length;  for(vari = 0; i < Len; i++) {            varTMP = This[i]; if(!temp.hasownproperty (TMP)) {//hasOwnProperty is used to determine whether an object has a property or object that you give a name totemp[ This[i]] = "Yes"; }} Len= 0; varTemparr = [];  for(varIinchtemp) {Temparr[len++] =i; }        returnTemparr; }

You're using hasOwnProperty, and that's a good efficiency.

Here is my test code:

functiondelrepeatexample () {vararr = [];//Array of tests         for(vari = 0; I < 10000; i++) {            vart = parseint (Math.random () * 1000); Arr[i]=(T.tostring ()); }        vars =NewDate ();//define the start time!        varA=arr.delrepeat (); varDD =NewDate ()-S;//Execution Time = Current time-start time}

10000 and 100000 long arrays were tested, and the test results were as follows:

Method One: 242ms (10000-length) 2240ms (100000-length)

Method Two: 6ms 39ms

Method Three: 255ms 2138ms

Method Four: 4ms 19ms

Method Five: 416ms 4492ms

Method Six: 9ms 50ms

Method Seven: 2ms 8ms

I personally prefer method two, method seven.

Not much to say, that's it ....

JS to remove the array duplicates

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.