javascript数字数组去重复项的实现代码


Posted in Javascript onDecember 30, 2010

test.htm

<!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,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();//数组很大时,最快 
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
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("<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]; 
} 
}
Javascript 相关文章推荐
用JavaScript脚本实现Web页面信息交互
Oct 11 Javascript
jquery之empty()与remove()区别说明
Sep 10 Javascript
JS的document.all函数使用示例
Dec 30 Javascript
jQuery异步加载数据并添加事件示例
Aug 24 Javascript
用jquery模仿的a的title属性的例子
Oct 22 Javascript
JavaScript使用Prototype实现面向对象的方法
Apr 14 Javascript
JS实现鼠标滑过显示边框的菜单效果
Sep 21 Javascript
JavaScript 下载svg图片为png格式
Jun 21 Javascript
超好用的jQuery分页插件jpaginate用法示例【附源码下载】
Dec 06 jQuery
js中call()和apply()改变指针问题的讲解
Jan 17 Javascript
JS字符串与二进制的相互转化实例代码详解
Jun 28 Javascript
vue编写简单的购物车功能
Jan 08 Vue.js
ExtJs的Date格式字符代码
Dec 30 #Javascript
jcarousellite.js 基于Jquery的图片无缝滚动插件
Dec 30 #Javascript
使用jQuery全局事件ajaxStart为特定请求实现提示效果的代码
Dec 30 #Javascript
在VS2008中使用jQuery智能感应的方法
Dec 30 #Javascript
jQuery在vs2008及js文件中的无智能提示的解决方法
Dec 30 #Javascript
js TextArea的选中区域处理
Dec 28 #Javascript
基于jquery的一行代码轻松实现拖动效果
Dec 28 #Javascript
You might like
php 的反射详解及示例代码
2016/08/25 PHP
Thinkphp5行为使用方法汇总
2017/12/21 PHP
php识别翻转iphone拍摄的颠倒图片
2018/05/17 PHP
如何在PHP中使用AES加密算法加密数据
2020/06/24 PHP
删除select中所有option选项jquery代码
2013/08/12 Javascript
JS二维数组的定义说明
2014/03/03 Javascript
JavaScript通过select动态更换图片的方法
2015/03/23 Javascript
JS设置cookie、读取cookie、删除cookie
2015/04/17 Javascript
JavaScript学习笔记整理之引用类型
2016/01/22 Javascript
理解JavaScript事件对象
2016/01/25 Javascript
AngularJS身份验证的方法
2016/02/17 Javascript
JavaScript实现相册弹窗功能(zepto.js)
2016/06/21 Javascript
js基于cookie记录来宾姓名的方法
2016/07/19 Javascript
jQuery中页面返回顶部的方法总结
2016/12/30 Javascript
jQuery实现优雅的弹窗效果(6)
2017/02/08 Javascript
详解Vue中过度动画效果应用
2017/05/25 Javascript
vue解决使用webpack打包后keep-alive不生效的方法
2018/09/01 Javascript
微信小程序引入模块中wxml、wxss、js的方法示例
2019/08/09 Javascript
js实现提交前对列表数据的增删改查
2020/01/16 Javascript
vue监听滚动事件的方法
2020/12/21 Vue.js
[00:16]热血竞技场
2019/03/06 DOTA
[49:15]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第二场 1月19日
2021/03/11 DOTA
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
Python 批量合并多个txt文件的实例讲解
2018/05/08 Python
Python不同目录间进行模块调用的实现方法
2019/01/29 Python
python基于opencv检测程序运行效率
2019/12/28 Python
HTML5 Canvas的事件处理介绍
2015/04/24 HTML / CSS
澳大利亚首个在线预订旅游网站:Wotif
2017/07/19 全球购物
家长会学生家长演讲稿
2013/12/29 职场文书
初中同学聚会感言
2014/02/11 职场文书
职业生涯规划书范文
2014/03/10 职场文书
一份恶作剧的检讨书
2014/09/13 职场文书
教师党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
春风化雨观后感
2015/06/11 职场文书
2015年重阳节主持词
2015/07/04 职场文书
各种货币符号快捷输入
2022/02/17 杂记