JS clears the string array, repeating elements

Source: Internet
Author: User
<script language= "JavaScript" > <!--var arrdata=new Array (); for (var i=0 i<1000; i++) {Arrdata[arrdata.length] = String.fromCharCode (Math.floor (Math.random () *26) +97); 

document.write (arrdata+ "<br/>"); Method one, normal traversal function Myarray_unique (myarray) {//var myarray=new Array ("A", "a", "C", "A", "C", "D", "E", "F", "F", "G", "H", "G"
"," H "," K ");
var Haha=myarray; for (Var i=0;i<myarray.length;i++) {for (Var j=0;j<myarray.length;j++) {Temp=myarray[i]; if ((I+j+1) < MYARRAY.LENGTH&AMP;&AMP;TEMP==MYARRAY[I+J+1]///If the current element is equal to the latter element Haha.splice (i+j+1,1);
Then remove the next element}} return haha; }//Method two function Getunique (Somearray) {temparray=somearray.slice (0);//copy array to temporary array for (Var i=0;i<temparray.length;i+
+) {for (Var j=i+1;j<temparray.length;) {if (Temparray[j]==temparray[i])//The following elements are deleted and counted if they are the same as they are to be compared;//delete, the following elements are automatically advanced, so the pointer J does not move {temparray.splice (j,1);} else {j+
+;
}//Is different, then the pointer moves} return temparray; }//Method three regular expressions--applicable to character array function getUnique2 (A) {var str = "\x0f" + a.join ("\x0f");
while (/(\w+) [^\1]*\1/.test (str)) str = str.replace ("\x0f" + regexp.$1, "");
return Str.substr (1). Split ("\x0f");
}//Method four-associative structure Array.prototype.unique = Array_unique; function Array_unique () {var o = new Object (); for (var i=0,j=0; i<this.length; i++) {if (typeof o[this[i]] = ' undef
Ined ') {O[this[i]] = j + +;}}
this.length = 0;
for (var key in O) {This[o[key]] = key;
var d = new Date (). GetTime ();
document.write (Myarray_unique (arrdata));
D = new Date (). GetTime ()-D; document.write ("<br/>2000 element method one algorithm meter time consuming" + D + "millisecond!<br/><br/>");
About 370ms~390ms var d = new Date (). GetTime ();
document.write (Getunique (arrdata));
D = new Date (). GetTime ()-D; document.write ("<br/>2000 element method two algorithm meter time consuming" + D + "millisecond!<br/><br/>");
About 360ms~380ms var d = new Date (). GetTime ();
document.write (GetUnique2 (arrdata));
D = new Date (). GetTime ()-D; document.write ("<br/>2000 element Regular expression method three algorithm meter time consuming" + D + "millisecond!<br/><br/>");/About 80ms var d = new Date(). GetTime ();
document.write (Arrdata.unique ());
D = new Date (). GetTime ()-D; document.write ("<br/>2000 Element association structure method four algorithm meter time consuming" + D + "millisecond!<br/><br/>");/About 0ms~10ms//-->  T>


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.