Implementation Code for repeated entries in javascript numeric Array

Source: Internet
Author: User

Test.htm
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> array-remove-repeate </title>
<Style>
. Tt {background-color: #006699; height: 3px; overflow: hidden ;}
</Style>

</Head>

<Body>
<Div class = "tt"> </div>
<Div class = "result" id = "result"> </div>
<Script>

If (! Console)
{
Var console = {};
Console. log = function (str) {alert (str );}
}

Array. prototype. unique1 = function (){
Var r = new Array ();
Label: for (var I = 0, n = this. length; I <n; I ++ ){
For (var x = 0, y = r. length; x <y; x ++ ){
If (r [x] = this [I]) {
Continue label;
}
}
R [r. length] = this [I];
}
Return r;
}

Array. prototype. unique2 = function (){
Return this. sort (). join (",,"). replace (/(, | ^) ([^,] +) (, \ 2) + (, | $)/g, "$1 $2 $4 "). replace (/, +/g ,","). replace (/, $ /,""). split (",");
}

Array. prototype. unique3 = function (){
Var temp = {}, len = this. length;
For (var I = 0; I <len; I ++ ){
Var tmp = this [I];
If (! Temp. hasOwnProperty (tmp )){
Temp [this [I] = "my god ";
}
}

Len = 0;
Var tempArr = [];
For (var I in temp ){
TempArr [len ++] = I;
}
Return tempArr;
}
Array. prototype. unique4 = function (){
Var temp = new Array ();
This. sort ();
For (I = 0; I <this. length; I ++ ){
If (this [I] = this [I + 1]) {
Continue;
}
Temp [temp. length] = this [I];
}
Return temp;

}


Var test = (function ()
{
Var arr2 = [];
For (var I = 0; I <2000; I ++)
{
Var t = I;
T = parseInt (Math. random () * 2000) + 1;
Arr2 [I] = (t. toString ());


}
// Arr2 = ["zhoujian", "zhou", "zhou"];
Return function (){
Return arr2;
// Return [1, 2, 3];
};


})();
Window. onload = function (){


//
Watch. start ("Cost times1 :");
Var arr = test ();
Console. log (arr. length );

Arr = arr. unique1 ();
Console. log (arr. length );

Watch. stop ();
//
Watch. start ("Cost times2 :");
Arr = test ();
Console. log (arr. length );


Arr = arr. unique2 ();

Console. log (arr. length );

Watch. stop ();
//
Watch. start ("Cost times3 :");
Arr = test ();
Console. log (arr. length );

Arr = arr. unique3 (); // the fastest speed when the array is large

Console. log (arr. length );

Watch. stop ();

//

Watch. start ("Cost times4 :");
Arr = test ();
Console. log (arr. length );
Arr = arr. unique4 ();
Console. log (arr. length );
Watch. stop ();
Watch. report ();

}
</Script>
</Body>
</Html>

Watch. js
Copy codeThe Code is as follows:
Var Watch = {
Result: [],
Guid:-1,
TotalTime: 0,
Start: function (title ){
This. result [++ this. guid] = [title | this. guid, new Date (). getTime ()];
},
Stop: function (){
Var r = this. result [this. guid];
Var t = new Date (). getTime ()-r [1];
This. totalTime + = t;
R [1] = t;
If (t & gt; = 10000 ){
Alert ("This code takes too long to run, you shoshould optimizate them .");
}
},
Report: function (parent ){
Var div = document. createElement ("div ");
Div. style. fontSize = "12px ";
Var str = [];
Str. push ("<p> <B> The total times: </B> <span style = 'color: # f00'>" + this. totalTime + "</span> ms. </p> ");
For (var I = 0, l = this. result. length; I <l; I ++ ){
If (this. result [I]. length> 1 ){
Str. push ("<p>" + "<span style = 'width: 200px; display: inline-block; background-color: # f7f7f7;-moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; '> "+" <span style = 'background-color: #0c0;-moz-border-radius: 3px;-webkit-border-radius: 3px; border-radius: 3px; width = "+ (this. totalTime = 0? This. totalTime: parseInt (200 * this. result [I] [1]/this. totalTime) + "px; display: inline-block; '>" + this. result [I] [1] + "</span>" + "</span> <span style = 'width: 150px; display: inline-block; '> "+ this. result [I] [0] + "</span>" + "</p> ");
} Else {
Str. push (this. result [I] [0]);
}
}
Div. innerHTML = str. join ("");
Parent = parent | document. body;
Parent. appendChild (div );
Div = null;

This. totalTime = 0;
This. guid =-1;
This. result = [];
},
Fns: function (){
Var a = arguments;
For (var I = 0, l = a. length; I <l; I ++ ){
This. start (a [I] [0]);
A [I] [1] ();
This. stop ();
}
},
ExecByTimes: function (fn, times, title ){
This. start (title );
While (times --){
Fn ();
}
This. stop ();
},
Print: function (str ){
This. result [++ this. guid] = [str];
}
}

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.