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 相关文章推荐
JS 创建对象(常见的几种方法)
Nov 03 Javascript
关于捕获用户何时点击window.onbeforeunload的取消事件
Mar 06 Javascript
jquery mobile changepage的三种传参方法介绍
Sep 13 Javascript
js隐式全局变量造成的bug示例代码
Apr 22 Javascript
jQuery 删除/替换DOM元素的几种方式
May 20 Javascript
原生JavaScript实现滚动条效果
Mar 24 Javascript
JavaScript判断页面加载完之后再执行预定函数的技巧
May 17 Javascript
JavaScript仿百度图片浏览效果
Nov 23 Javascript
jQuery实现select模糊查询(反射机制)
Jan 14 Javascript
JavaScript中的普通函数和箭头函数的区别和用法详解
Mar 21 Javascript
mpvue性能优化实战技巧(小结)
Apr 17 Javascript
jQuery实现的图片点击放大缩小功能案例
Jan 02 jQuery
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中基于ts与nts版本- vc6和vc9编译版本的区别详解
2013/04/26 PHP
PHP中怎样防止SQL注入分析
2014/10/23 PHP
PHP防止刷新重复提交页面的示例代码
2015/11/11 PHP
通过PHP自带的服务器来查看正则匹配结果的方法
2015/12/24 PHP
php实现微信支付之企业付款
2018/05/30 PHP
PHP程序员学习使用Swoole的理由
2018/06/24 PHP
prototype Element学习笔记(篇二)
2008/10/26 Javascript
js监听键盘事件示例代码
2013/07/26 Javascript
js图片轮播特效代码分享
2015/09/07 Javascript
AngularJS身份验证的方法
2016/02/17 Javascript
Web打印解决方案之证件套打的实现思路
2016/08/29 Javascript
Vue.js实战之利用vue-router实现跳转页面
2017/04/01 Javascript
详解使用angularjs的ng-options时如何设置默认值(初始值)
2017/07/18 Javascript
Chrome调试折腾记之JS断点调试技巧
2017/09/11 Javascript
vue组件传递对象中实现单向绑定的示例
2018/02/28 Javascript
Vuejs监听vuex中值的变化的方法示例
2018/12/02 Javascript
微信小程序实现录音时的麦克风动画效果实例
2019/05/18 Javascript
JS函数动态传递参数的方法分析【基于arguments对象】
2019/06/05 Javascript
angularjs1.X 重构controller 的方法小结
2019/08/15 Javascript
Python常用的文件及文件路径、目录操作方法汇总介绍
2015/05/21 Python
Flask框架的学习指南之开发环境搭建
2016/11/20 Python
Python通过paramiko远程下载Linux服务器上的文件实例
2018/12/27 Python
解决Django删除migrations文件夹中的文件后出现的异常问题
2019/08/31 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
2019/09/06 Python
django中的图片验证码功能
2019/09/18 Python
基于Python和PyYAML读取yaml配置文件数据
2020/01/13 Python
PyQt5中向单元格添加控件的方法示例
2020/03/24 Python
html5定制表单_动力节点Java学院整理
2017/07/11 HTML / CSS
HTML5实现表单自动验证功能实例代码
2017/01/11 HTML / CSS
资生堂英国官网:Shiseido英国
2020/12/30 全球购物
成教毕业生自我鉴定
2013/10/23 职场文书
班级活动策划书
2014/02/06 职场文书
临时租车协议范本
2014/09/23 职场文书
警示教育观后感
2015/06/17 职场文书
班级元旦晚会开幕词
2016/03/04 职场文书
Spring Boot项目如何优雅实现Excel导入与导出功能
2022/06/10 Java/Android