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 Animation实现CSS3动画示例介绍
Aug 14 Javascript
Eclipse去除js(JavaScript)验证错误
Feb 11 Javascript
AngularJS中的$watch(),$digest()和$apply()区分
Apr 04 Javascript
基于JS实现EOS隐藏错误提示层代码
Apr 25 Javascript
Highcharts入门之基本属性
Aug 02 Javascript
Node.js批量给图片加水印的方法
Nov 15 Javascript
React Native验证码倒计时工具类分享
Oct 24 Javascript
微信小程序websocket实现聊天功能
Mar 30 Javascript
微信小程序学习笔记之跳转页面、传递参数获得数据操作图文详解
Mar 28 Javascript
vue中的面包屑导航组件实例代码
Jul 01 Javascript
解决Vue项目中tff报错的问题
Oct 21 Javascript
Vue组件化(ref,props, mixin,.插件)详解
May 15 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操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
一个PHP的ZIP压缩类分享
2014/05/04 PHP
php实现最简单的MVC框架实例教程
2014/09/08 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
2016/11/15 PHP
Extjs Ext.MessageBox.confirm 确认对话框详解
2010/04/02 Javascript
location.href 在IE6中不跳转的解决方法与推荐使用代码
2010/07/08 Javascript
用jQuery中的ajax分页实现代码
2011/09/20 Javascript
给Flash加一个超链接(推荐使用透明层)兼容主流浏览器
2013/06/09 Javascript
使用AOP改善javascript代码
2015/05/01 Javascript
JavaScript模块化开发之SeaJS
2015/12/13 Javascript
jQuery常用的一些技巧汇总
2016/03/26 Javascript
javascript日期比较方法实例分析
2016/06/17 Javascript
轻松实现js弹框显示选项
2016/09/13 Javascript
使用JavaScript实现表格编辑器(实例讲解)
2017/08/02 Javascript
PHP 实现一种多文件上传的方法
2017/09/20 Javascript
angular 内存溢出的问题解决
2018/07/12 Javascript
解决ie11 SCRIPT5011:不能执行已释放Script的代码问题
2019/05/05 Javascript
nodejs log4js 使用详解
2019/05/31 NodeJs
JS+CSS+HTML实现“代码雨”类似黑客帝国文字下落效果
2020/03/17 Javascript
简单了解JavaScript arguement原理及作用
2020/05/28 Javascript
如何在Vue项目中添加接口监听遮罩
2021/01/25 Vue.js
[03:07]【DOTA2亚洲邀请赛】我们,梦开始的地方
2017/03/07 DOTA
python实现控制台打印的方法
2019/01/12 Python
Python Flask框架扩展操作示例
2019/05/03 Python
python 爬取疫情数据的源码
2020/02/09 Python
Pycharm中切换pytorch的环境和配置的教程详解
2020/03/13 Python
python删除指定列或多列单个或多个内容实例
2020/06/28 Python
python的dict判断key是否存在的方法
2020/12/09 Python
HTML5 中新的全局属性(整理)
2013/07/31 HTML / CSS
素食餐饮项目创业计划书
2014/02/02 职场文书
酒店值班经理的工作职责范本
2014/02/18 职场文书
新郎结婚保证书
2015/02/26 职场文书
2015年学生会个人工作总结
2015/04/09 职场文书
员工辞退通知书
2015/04/17 职场文书
学校光盘行动倡议书
2015/04/28 职场文书
2015年挂职锻炼个人总结
2015/10/22 职场文书