js高效率数组去重
时间:2015-06-05 22:47:45
收藏:0
阅读:313
为数组对象添加数组去重方法,并且返回删除的数组元素:
Array.prototype.clearRedurance=function(){ var newArray=[],// redurance=[],// i,// length; this.sort(function(a,b){ return a>b ? 1:(a<b ? -1:0); });//数组先排序 newArray.push(this[0]); for(i=0,length=this.length;i<length;i++){ if(newArray[newArray.length-1]!=this[i]){ newArray.push(this[i]); }else{ redurance.push(this[i]); this.splice(i,1); i--; length--; } } return redurance; }算法事件复杂度为O(n)。
原文:http://blog.csdn.net/vuturn/article/details/46380153
评论(0)