Implementation Code for repeated items in javascript numeric array _ javascript skills

Source: Internet
Author: User
Console. log does not support ie. the following code needs to be tested in Firefox, otherwise there will be problems. Test.htm

The Code is as follows:






Array-remove-repeate







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



Watch. js

The 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 p = document. createElement ("p ");
P. style. fontSize = "12px ";
Var str = [];
Str. push ("

The total times:"+ This. totalTime +" ms.

");
For (var I = 0, l = this. result. length; I <l; I ++ ){
If (this. result [I]. length> 1 ){
Str. push ("

"+" "+" "+ This. result [I] [1] +" "+" "+ this. result [I] [0] +" "+"

");
} Else {
Str. push (this. result [I] [0]);
}
}
P. innerHTML = str. join ("");
Parent = parent | document. body;
Parent. appendChild (p );
P = 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.