数组去重

原创 2016年12月31日 11:51:37
面试时经常被问到数组去重的问题,个人觉得这种问题其实没太大的作用,毕竟在实际工作中很少能遇到的很大的数组,所以不论以什么样的方式实现都很难做到内存的溢出及产生较大的耗时差距(除非你是故意),这里以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用时最长

数组的去重、虑空

  • 2014年06月30日 15:19
  • 641B
  • 下载

数组随机赋值 出重与排序问题

  • 2012年12月19日 21:17
  • 535B
  • 下载

C#二维数组的定义,初始化(重在理解两者的区别)

对比C#二维数组本身两种不同的定义和初始化, 同时对比C++对二维数组。

list和数组去重,记录重复次数

  • 2017年11月03日 08:56
  • 3KB
  • 下载

C++数组元素 排序 去重

说明 注释都写在程序中了 /**************************************************************************/ // 程序:排序去重...

JavaScript Array数组去重源码下载

  • 2017年06月29日 15:40
  • 2KB
  • 下载

重看笔记10.7 模块、数组.rar

  • 2010年10月25日 19:18
  • 4KB
  • 下载

nodejs通过lodash合并去重由unixtime和Date组成的两个数组

1. 问题起源 最近在实现一个API,其中有一部分功能是需要从Mongodb中取出一个由Date对象组成的数组,然后将客户端传过来的unixtime合并到该数组中,并且去重复。 比如,假设从m...

一个数组去重的小程序

  • 2015年03月15日 13:49
  • 438B
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:数组去重
举报原因:
原因补充:

(最多只允许输入30个字)