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 相关文章推荐
解放web程序员的输入验证
Oct 06 Javascript
jquery 插件 任意位置浮动固定层
Dec 25 Javascript
jQuery EasyUI API 中文文档 - Documentation 文档
Sep 29 Javascript
JS无法捕获滚动条上的mouse up事件的原因猜想
Mar 21 Javascript
jquery+php实现搜索框自动提示
Nov 28 Javascript
js实现jquery的offset()方法实例
Jan 10 Javascript
jQuery Dialog 取消右上角删除按钮事件
Sep 07 Javascript
jQuery 实现倒计时天,时,分,秒功能
Jul 31 jQuery
详解angular应用容器化部署
Aug 14 Javascript
浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
Dec 20 Javascript
微信小程序 如何保持登录状态
Aug 16 Javascript
小程序简单两栏瀑布流效果的实现
Dec 18 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 CURL获取返回值的方法
2014/05/04 PHP
php文件夹的创建与删除方法
2015/01/24 PHP
php中给js数组赋值方法
2014/03/10 Javascript
JavaScript获取数组最小值和最大值的方法
2015/06/09 Javascript
javascript每日必学之多态
2016/02/23 Javascript
js阻止浏览器默认行为触发的通用方法(推荐)
2016/05/15 Javascript
jquery实现百叶窗效果
2017/01/12 Javascript
webpack4 升级迁移的实现
2018/09/12 Javascript
bootstrap-table实现表头固定以及列固定的方法示例
2019/03/07 Javascript
vue vantUI tab切换时 list组件不触发load事件的问题及解决方法
2020/02/14 Javascript
JS前端面试必备——基本排序算法原理与实现方法详解【插入/选择/归并/冒泡/快速排序】
2020/02/24 Javascript
python中mechanize库的简单使用示例
2014/01/10 Python
Python入门篇之编程习惯与特点
2014/10/17 Python
Python脚本判断 Linux 是否运行在虚拟机上
2015/04/25 Python
Python3读取UTF-8文件及统计文件行数的方法
2015/05/22 Python
python如何实现excel数据添加到mongodb
2015/07/30 Python
python 检查文件mime类型的方法
2018/12/08 Python
python全栈知识点总结
2019/07/01 Python
django 简单实现登录验证给你
2019/11/06 Python
pandas中read_csv的缺失值处理方式
2019/12/19 Python
Python telnet登陆功能实现代码
2020/04/16 Python
Python列表去重复项的N种方法(实例代码)
2020/05/12 Python
python 怎样进行内存管理
2020/11/10 Python
selenium自动化测试入门实战
2020/12/21 Python
什么是"引用"?申明和使用"引用"要注意哪些问题?
2016/03/03 面试题
应届生个人求职信模板
2013/11/26 职场文书
财政局长自荐信范文
2013/12/22 职场文书
计算机求职自荐信范文
2014/04/19 职场文书
大二学生自我检讨书
2014/10/23 职场文书
车间主任岗位职责范本
2015/04/08 职场文书
学习雷锋精神倡议书
2015/04/27 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
2015年社区居委会工作总结
2015/05/18 职场文书
汤姆索亚历险记读书笔记
2015/06/29 职场文书
2016年小学党支部创先争优活动总结
2016/04/05 职场文书
星际争霸 Light vs Action 一场把教主看到鬼畜的比赛
2022/04/01 星际争霸