关闭

数组去重

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


执行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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1703次
    • 积分:60
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章分类
    文章存档