数组去重的两种方式es5数组去重、es6数组去重。不同方法所花时间对比

let sliceHaveArr = arr => {
            let item = []
            const lengths = arr.length
            for (let i = 0; i < lengths; i++) {
                for (let y = i + 1; i < lengths; i++) {
                    if (arr[i] == arr[y]) {
                        console.log(arr[i], arr[y], y)

                        arr.splice(y, 1)
                    } else {
                        item.push(arr[i])
                    }
                }
            }
            return arr
        }
        console.time(1)
        sliceHaveArr([1, 2, 4, 2, 6])
        console.timeEnd(1)
        let spliceRepeat = arr => {
            let getArr = {}
            return arr.filter(item => {
                return getArr.hasOwnProperty(typeof item + item) ? false : (getArr[typeof item + item] = true)
            })
        }

        console.time(1)
        spliceRepeat([1, 2, 3, 4, 5, 5, 3, 6])
        console.timeEnd(1)

 

posted @ 2020-08-20 14:03  问问大将军  阅读(307)  评论(0编辑  收藏  举报