数组去重

面试时经常被问到数组去重的问题,个人觉得这种问题其实没太大的作用,毕竟在实际工作中很少能遇到的很大的数组,所以不论以什么样的方式实现都很难做到内存的溢出及产生较大的耗时差距(除非你是故意),这里以2的13次方乘以10个数组项为例,在不改变原数组的前提下,做了几种没考虑效率及内存的方法,仅以实现为目的


        {
            function createTestArray(){
                const testArray=[1,2,3,4,5,'a','b','c','d','e'];
                for(let i=0;i<13;i++){
                    testArray.push(...testArray);
                }
                testArray.sort((a,b)=>Math.random()>0.5?1:-1);
                //console.log(`testArray:${testArray},length:${testArray.length}`);
                return testArray;
            }
            const testArray=createTestArray();

            function showTime(fun){
                const start=new Date();
                const result=fun();
                return `name:${fun.name},time:${new Date()-start},result:${result.join(',')}`;
            }

            function removeRepeatBySet(){
                return [...new Set(testArray)];
            }

            function removeRepeatByFilter(){
                return testArray.filter((item,index,arr)=>arr.indexOf(item)==index);
            }

            function removeRepeatByForof(){
                const obj={},arr=[],value={};
                for(let i of testArray){
                    if(obj[i]==value){
                        continue;
                    }else if(obj[i]!=value){
                        obj[i]=value;
                        arr.push(i);
                    }
                }
                return arr;
            }

            function removeRepeatByFor(){
                const obj={},arr=[],value={};
                for(let i= 0,len=testArray.length;i
  
  


执行10次的执行结果


[
    "name:removeRepeatByFilter,time:10,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:10,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:12,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:6,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:7,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:7,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:8,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFilter,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:2,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:3,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:4,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:5,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByFor,time:6,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:11,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:6,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:6,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:7,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:8,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatByForof,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:10,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:13,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:16,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:6,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:7,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:7,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:8,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d",
    "name:removeRepeatBySet,time:9,result:3,5,a,b,1,4,c,2,e,d"
]


从执行时间上看,for循环是用时最少的,其次for of,filter,而es6的Set用时最长

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值