let a1 = [1,2,3,2,3,undefined, NaN, new Object(), 8,8, new Date(), new Date(), new RegExp()]
let a2 = []
console.time('time2')
console.log([...new Set(a1)])
console.timeEnd('time2')
function unique(array) {
if(array.length === 0) {
return a2
}
let first = array[0]
for(let i = 1; i < array.length; i++) {
if(first === array[i]) {
array.splice(i, 1)
}
}
a2.push(first)
a1.splice(0, 1)
return unique(array)
}
console.time('time1')
unique(a1)
console.timeEnd('time1')
javascript es6数组去重和手写递归数组去重,以及两种去重的耗时对比
最新推荐文章于 2024-07-12 16:27:40 发布