其???上搜索?些方法一堆堆的,之所以????一遍主要是因??自己??T一?之後就忘了其他方法怎麽???,就??一????一下,?便做???y?看看哪??效率最高,??了更好展示效果,我??瓤?????比?好的方法,後面?一?y?。(?剀疤崾荆合挛男}是??了?便,一般情?r下不建???在原型上,容易污染全局)
一,通??ふ?ο?傩?/strong>
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6]; Array.prototype.unique1 = function(){ var obj = {}, ary = [], i= 0, len = this.length; for(; i<len; i++){ if(!obj[this[i]]){ obj[this[i]] = 1; ary.push(this[i]); } } return ary.sort(function(a,b){return a - b}); } console.log(n.unique1());
二,通??ふ?到M位置
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6]; Array.prototype.unique2 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(ary.indexOf(this[i]) == -1) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } console.log(n.unique2());
三,跟?ふ?到M位置?似,搜索?底值谝淮纬霈F的位置是不是跟?前位置一??/strong>
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6]; Array.prototype.unique3 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this.indexOf(this[i]) == i) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } console.log(n.unique3());
四,比?常?,先排序,再比?前後????底质遣皇窍嗟?/strong>
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6]; Array.prototype.unique4 = function(){ this.sort(function(a,b){return a - b}); var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this[i] !== this[i-1]) ary.push(this[i]); } return ary; } console.log(n.unique4());
接下?硎蔷W上比?少的效率比?了,也是本文重?,我??先生成一????r函?到y一比?
首先生成一??一百?底值?到M
Array.prototype.unique1 = function(){ var obj = {}, ary = [], i= 0, len = this.length; for(; i<len; i++){ if(!obj[this[i]]){ obj[this[i]] = 1; ary.push(this[i]); } } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique2 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(ary.indexOf(this[i]) == -1) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique3 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this.indexOf(this[i]) == i) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique4 = function(){ this.sort(function(a,b){return a - b}); var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this[i] !== this[i-1]) ary.push(this[i]); } return ary; } function randomAry (n) { var ary = [],i=0; for(; i<n; i++){ ary.push(Math.ceil(Math.random()*10000)); } console.log(ary) return ary; } function useTime (fn) { var start = new Date(); fn(); var end = new Date(); console.log('本次函?颠\行花了:' + (end - start) + '毫秒'); } var ary = randomAry(100), fn1 = function(){ ary.unique1() }, fn2 = function(){ ary.unique2() }, fn3 = function(){ ary.unique3() }, fn4 = function(){ ary.unique4() }; useTime(fn1); useTime(fn2); useTime(fn3); useTime(fn4);
?果在我的谷歌?g?器??
方法一:0毫秒
方法二:1毫秒
方法三:0毫秒
方法四:00毫秒
(嗯,果然?代?g?器强大?o比,不吐槽??g?器了)
看看1000???底?/p>
Array.prototype.unique1 = function(){ var obj = {}, ary = [], i= 0, len = this.length; for(; i<len; i++){ if(!obj[this[i]]){ obj[this[i]] = 1; ary.push(this[i]); } } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique2 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(ary.indexOf(this[i]) == -1) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique3 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this.indexOf(this[i]) == i) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique4 = function(){ this.sort(function(a,b){return a - b}); var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this[i] !== this[i-1]) ary.push(this[i]); } return ary; } function randomAry (n) { var ary = [],i=0; for(; i<n; i++){ ary.push(Math.ceil(Math.random()*10000)); } console.log(ary) return ary; } function useTime (fn) { var start = new Date(); fn(); var end = new Date(); console.log('本次函?颠\行花了:' + (end - start) + '毫秒'); } var ary = randomAry(1000), fn1 = function(){ ary.unique1() }, fn2 = function(){ ary.unique2() }, fn3 = function(){ ary.unique3() }, fn4 = function(){ ary.unique4() }; useTime(fn1); useTime(fn2); useTime(fn3); useTime(fn4); 方法一:1-2毫秒 方法二:40-50毫秒 方法三:40-50毫秒 方法四:0-1毫秒 看看10000???底郑????大,?始吃不消了,等??五六秒吧) Array.prototype.unique1 = function(){ var obj = {}, ary = [], i= 0, len = this.length; for(; i<len; i++){ if(!obj[this[i]]){ obj[this[i]] = 1; ary.push(this[i]); } } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique2 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(ary.indexOf(this[i]) == -1) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique3 = function(){ var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this.indexOf(this[i]) == i) ary.push(this[i]); } return ary.sort(function(a,b){return a - b}); } Array.prototype.unique4 = function(){ this.sort(function(a,b){return a - b}); var ary = [], i= 0, len = this.length; for(; i<len; i++){ if(this[i] !== this[i-1]) ary.push(this[i]); } return ary; } function randomAry (n) { var ary = [],i=0; for(; i<n; i++){ ary.push(Math.ceil(Math.random()*10000)); } console.log(ary) return ary; } function useTime (fn) { var start = new Date(); fn(); var end = new Date(); console.log('本次函?颠\行花了:' + (end - start) + '毫秒'); } var ary = randomAry(10000), fn1 = function(){ ary.unique1() }, fn2 = function(){ ary.unique2() }, fn3 = function(){ ary.unique3() }, fn4 = function(){ ary.unique4() }; useTime(fn1); useTime(fn2); useTime(fn3); useTime(fn4);
因??????大,我?拿一次??????喜?g就刷新?状伪容^?h
方法一:10毫秒
方法二:1258毫秒
方法三:2972毫秒
方法四:5毫秒
原本?想?更大的???纯矗?会崾?f??我的?面就奔?⒘恕!!?⒘恕!!A?/p>
然後??五???,也奔?⒘恕!!?⒘恕!!A?/p>
那就算吧,
反正在一??????Y面?Ρ龋???酱螅?椒ㄒ桓?椒ㄋ陌春撩脒f增,方法二跟方法三效率感人
效率?算:4>1>2>3
方法?算: 先排序,再比?前後????底质遣皇窍嗟 > 通??ふ?ο?傩 > 通??ふ?到M位置 > 搜索?底值谝淮纬霈F的位置是不是跟?前位置一??/p>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!
js实现数组去重方法及效率?Ρ? target=
- Author -
Lin_Grady声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@