數(shù)組去重高效方法

一、測(cè)試模版

數(shù)組去重是一個(gè)老生常談的問(wèn)題,網(wǎng)上流傳著有各種各樣的解法

為了測(cè)試這些解法的性能,我寫(xiě)了一個(gè)測(cè)試模版,用來(lái)計(jì)算數(shù)組去重的耗時(shí)

// distinct.jslet arr1 = Array.from(newArray(100000), (x, index)=>{

? ? return index

})

let arr2 = Array.from(newArray(50000), (x, index)=>{

? ? returnindex+index

})

let start =new Date().getTime()

console.log('開(kāi)始數(shù)組去重')

function distinct(a, b) {

? ? // 數(shù)組去重}

console.log('去重后的長(zhǎng)度', distinct(arr1, arr2).length)

let end =new Date().getTime()

console.log('耗時(shí)', end - start)

這里分別創(chuàng)建了兩個(gè)長(zhǎng)度為 10W 和 5W 的數(shù)組

然后通過(guò) distinct() 方法合并兩個(gè)數(shù)組,并去掉其中的重復(fù)項(xiàng)

數(shù)據(jù)量不大也不小,但已經(jīng)能說(shuō)明一些問(wèn)題了


二、Array.filter() + indexOf

這個(gè)方法的思路是,將兩個(gè)數(shù)組拼接為一個(gè)數(shù)組,然后使用 ES6 中的Array.filter()遍歷數(shù)組,并結(jié)合 indexOf 來(lái)排除重復(fù)項(xiàng)

function distinct(a, b) {

? ? let arr = a.concat(b);

? ? returnarr.filter((item, index)=> {

? ? ? ? returnarr.indexOf(item) === index

? ? })

}

這就是我被吐槽的那個(gè)數(shù)組去重方法,看起來(lái)非常簡(jiǎn)潔,但實(shí)際性能。。。

是的,現(xiàn)實(shí)就是這么殘酷,處理一個(gè)長(zhǎng)度為 15W 的數(shù)組都需要 8427ms


三、雙重 for 循環(huán)

最容易理解的方法,外層循環(huán)遍歷元素,內(nèi)層循環(huán)檢查是否重復(fù)

當(dāng)有重復(fù)值的時(shí)候,可以使用 push(),也可以使用 splice()

function distinct(a, b) {

? ? let arr = a.concat(b);

? ? for(let i=0, len=arr.length; i

? ? ? ? for(let j=i+1; j

? ? ? ? ? ? if(arr[i] == arr[j]) {

? ? ? ? ? ? ? ? arr.splice(j, 1);

? ? ? ? ? ? ? ? // splice 會(huì)改變數(shù)組長(zhǎng)度,所以要將數(shù)組長(zhǎng)度 len 和下標(biāo) j 減一len--;? ? ? ? ? ? ? ? j--;? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? return arr

}

但這種方法占用的內(nèi)存較高,效率也是最低的


四、for...of +?includes()

雙重for循環(huán)的升級(jí)版,外層用 for...of 語(yǔ)句替換 for 循環(huán),把內(nèi)層循環(huán)改為?includes()

先創(chuàng)建一個(gè)空數(shù)組,當(dāng) includes()?返回 false 的時(shí)候,就將該元素 push 到空數(shù)組中?

類(lèi)似的,還可以用 indexOf() 來(lái)替代 includes()

function distinct(a, b) {

? ? let arr = a.concat(b)

? ? let result = []

? ? for (let i of arr) {

? ? ? ? !result.includes(i) && result.push(i)

? ? }

? ? return result

}

這種方法和 filter + indexOf 挺類(lèi)似

只是把 filter() 的內(nèi)部邏輯用 for 循環(huán)實(shí)現(xiàn)出來(lái),再把 indexOf 換為 includes

所以時(shí)長(zhǎng)上也比較接近


五、Array.sort()

首先使用 sort() 將數(shù)組進(jìn)行排序

然后比較相鄰元素是否相等,從而排除重復(fù)項(xiàng)

function distinct(a, b) {

? ? let arr = a.concat(b)

? ? arr = arr.sort()

? ? let result = [arr[0]]

? ? for(let i=1, len=arr.length; i

? ? ? ? arr[i] !== arr[i-1] && result.push(arr[i])

? ? }

? ? return result

}

這種方法只做了一次排序和一次循環(huán),所以效率會(huì)比上面的方法都要高


六、new Set()

ES6 新增了Set這一數(shù)據(jù)結(jié)構(gòu),類(lèi)似于數(shù)組,但 Set 的成員具有唯一性

基于這一特性,就非常適合用來(lái)做數(shù)組去重了

function distinct(a, b) {

? ? returnArray.from(new Set([...a, ...b]))

}

那使用 Set 又需要多久時(shí)間來(lái)處理 15W 的數(shù)據(jù)呢?

喵喵喵??? 57ms ??我沒(méi)眼花吧??

然后我在兩個(gè)數(shù)組長(zhǎng)度后面分別加了一個(gè)0,在 150W 的數(shù)據(jù)量之下...

居然有如此高性能且簡(jiǎn)潔的數(shù)組去重辦法?!


七、for...of + Object

這個(gè)方法我只在一些文章里見(jiàn)過(guò),實(shí)際工作中倒沒(méi)怎么用

首先創(chuàng)建一個(gè)空對(duì)象,然后用 for 循環(huán)遍歷

利用對(duì)象的屬性不會(huì)重復(fù)這一特性,校驗(yàn)數(shù)組元素是否重復(fù)

function distinct(a, b) {

? ? let arr = a.concat(b)

? ? let result = []

? ? let obj = {}

? ? for (let i of arr) {

? ? ? ? if(!obj[i]) {

? ? ? ? ? ? result.push(i)

? ? ? ? ? ? obj[i] = 1? ? ? ? }

? ? }

? ? return result

}

當(dāng)我看到這個(gè)方法的處理時(shí)長(zhǎng),我又傻眼了

15W 的數(shù)據(jù)居然只要 16ms ??? 比 Set() 還快???

然后我又試了試 150W 的數(shù)據(jù)量...

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容