Home > Web Front-end > JS Tutorial > body text

Implementation code for removing duplicate items from javascript numeric array_javascript skills

WBOY
Release: 2016-05-16 18:12:57
Original
1123 people have browsed it
test.htm
复制代码 代码如下:





array-remove-repeate







<script> <br><br>if(!console) <br>{ <br>var console={}; <br>console.log=function(str){alert(str);} <br>} <br><br>Array.prototype.unique1 = function () { <br>var r = new Array(); <br>label:for(var i = 0, n = this.length; i < n; i ) { <BR>for(var x = 0, y = r.length; x < y; x ) { <BR>if(r[x] == this[i]) { <BR>continue label; <BR>} <BR>} <BR>r[r.length] = this[i]; <BR>} <BR>return r; <BR>} <br><br>Array.prototype.unique2 = function () { <BR>return this.sort().join(",,").replace(/(,|^)([^,] )(,,2) (,|$)/g,"$1$2$4").replace(/,, /g,",").replace(/,$/,"").split(","); <BR>} <br><br>Array.prototype.unique3 = function() { <BR>var temp = {}, len = this.length; <BR>for(var i=0; i < len; i ) { <BR>var tmp = this[i]; <BR>if(!temp.hasOwnProperty(tmp)) { <BR>temp[this[i]] = "my god"; <BR>} <BR>} <br><br>len = 0; <BR>var tempArr=[]; <BR>for(var i in temp) { <BR>tempArr[len ] = i; <BR>} <BR>return tempArr; <BR>} <BR>Array.prototype.unique4 = function () { <BR>var temp = new Array(); <BR>this.sort(); <BR>for(i = 0; i < this.length; i ) { <BR>if( this[i] == this[i 1]) { <BR>continue; <BR>} <BR>temp[temp.length]=this[i]; <BR>} <BR>return temp; <br><br>} <br><br><BR>var test=(function() <BR>{ <BR>var arr2=[]; <BR>for(var i=0;i<2000;i ) <BR>{ <BR>var t=i; <BR>t=parseInt(Math.random()*2000) 1; <BR>arr2[i]=(t.toString()); <br><br><BR>} <BR>//arr2=["zhoujian","zhou","zhou"]; <BR>return function(){ <BR>return arr2; <BR>//return [1,2,3,3]; <BR>}; <br><br><BR>})(); <BR>window.onload=function(){ <br><br><BR>// <BR>Watch.start("Cost times1:"); <BR>var arr= test(); <BR>console.log(arr.length ); <br><br>arr=arr.unique1(); <BR>console.log(arr.length); <br><br>Watch.stop(); <BR>// <BR>Watch.start("Cost times2:"); <BR>arr= test(); <BR>console.log(arr.length); <br><br><BR>arr=arr.unique2(); <br><br>console.log(arr.length); <br><br>Watch.stop(); <BR>// <BR>Watch.start("Cost times3:"); <BR>arr= test(); <BR>console.log(arr.length ); <br><br>arr=arr.unique3();//数组很大时,最快 <br><br>console.log(arr.length ); <br><br>Watch.stop(); <br><br>// <br><br>Watch.start("Cost times4:"); <BR>arr= test(); <BR>console.log(arr.length); <BR>arr=arr.unique4(); <BR>console.log(arr.length); <BR>Watch.stop(); <BR>Watch.report(); <br><br>} <BR></script>



Watch.js
复制代码 代码如下:

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>=10000){
alert("This code takes too long to run,you should optimizate them.");
}
},
report: function(parent){
var div = document.createElement("div");
div.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]);
}
}
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];
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template