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 相关文章推荐
jquery validate 自定义验证方法介绍 日期验证
Feb 27 Javascript
分享2个jQuery插件--jquery.fileupload与artdialog
Dec 26 Javascript
javascript中scrollTop详解
Apr 13 Javascript
js实现非常简单的焦点图切换特效实例
May 07 Javascript
Javascript OOP之面向对象
Jul 31 Javascript
Bootstrap Navbar Component实现响应式导航
Oct 08 Javascript
关于foreach循环中遇到的问题小结
May 08 Javascript
微信小程序日期时间选择器使用方法
Feb 01 Javascript
vue: WebStorm设置快速编译运行的方法
Oct 18 Javascript
微信小程序常用简易小函数总结
Feb 01 Javascript
react的滑动图片验证码组件的示例代码
Feb 27 Javascript
Javascript 关于基本类型和引用类型的个人理解
Nov 01 Javascript
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实现百度网盘图片直链的代码分享
2012/11/01 PHP
PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码
2013/02/14 PHP
深入array multisort排序原理的详解
2013/06/18 PHP
关于更改Zend Studio/Eclipse代码风格主题的介绍
2013/06/23 PHP
php简单统计在线人数的方法
2016/05/10 PHP
CSS+JS构建的图片查看器
2006/07/22 Javascript
jquery移动listbox的值原理及代码
2013/05/03 Javascript
jqgrid 编辑添加功能详细解析
2013/11/08 Javascript
JQuery中$(document)是什么意思有什么作用
2014/07/21 Javascript
Sort()函数的多种用法
2016/03/20 Javascript
微信小程序 出现47001 data format error原因解决办法
2017/03/10 Javascript
基于Node的React图片上传组件实现实例代码
2017/05/10 Javascript
jQuery+ajax实现局部刷新的两种方法
2017/06/08 jQuery
Vue.js实现网格列表布局转换方法
2017/08/25 Javascript
javascript中函数的写法实例代码详解
2018/10/28 Javascript
性能优化篇之Webpack构建速度优化的建议
2019/04/03 Javascript
Vue基本使用之对象提供的属性功能
2019/04/30 Javascript
vue-video-player 解决微信自动全屏播放问题(横竖屏导致样式错乱问题)
2020/02/25 Javascript
Layui弹框中数据表格中可双击选择一条数据的实现
2020/05/06 Javascript
[01:31](回顾)杀出重围,决战TI之巅
2014/07/01 DOTA
python encode和decode的妙用
2009/09/02 Python
Python 支持向量机分类器的实现
2020/01/15 Python
python爬虫库scrapy简单使用实例详解
2020/02/10 Python
python实现引用其他路径包里面的模块
2020/03/09 Python
Python实现的北京积分落户数据分析示例
2020/03/27 Python
keras 实现轻量级网络ShuffleNet教程
2020/06/19 Python
canvas实现手机的手势解锁的步骤详细
2020/03/16 HTML / CSS
美国精品家居用品网站:US-Mattress
2016/08/24 全球购物
食品业务员岗位职责
2014/03/18 职场文书
中学学校门卫岗位职责
2014/08/15 职场文书
老人再婚离婚协议书范本
2014/10/27 职场文书
公司财务管理制度
2015/08/04 职场文书
《检阅》教学反思
2016/02/22 职场文书
《折线统计图》教学反思
2016/02/22 职场文书
看古人们是如何赞美老师的?
2019/07/08 职场文书
使用JS前端技术实现静态图片局部流动效果
2022/08/05 Javascript