# 数组去重

{
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

[
"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"
]

• 本文已收录于以下专栏：

## 数组去重的五种方法

• St_Sp_En
• 2017年04月18日 17:49
• 1095

## 数组去重（c语言实现）

• zinss26914
• 2012年10月16日 00:03
• 16458

## C/C++面试之算法系列－－去除数组中的重复数字

去除数组中的重复数字 Sailor_forever  sailing_9806@163.com 转载请注明http://blog.csdn.net/sailor_8318/archive/200...
• sailor_8318
• 2008年10月12日 03:07
• 17353

## C# asp.net中字符串数组去重

public static void Purge(ref ListneedToPurge)        {                        for(int i=0;i        ...
• chunquanwang
• 2010年04月21日 13:42
• 1313

## 对一个数组的数进行 去重

int n = 11; int a[] = { 1, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5 }; n = unique(a, a + n) - a;//使用STL自带去重函数，返回...
• chuanyu
• 2016年11月11日 18:44
• 879

## 数组去重，简短而有效的方法

var arr = [1,2,3,4,5,6,7,8,1,2,3,4,5,12,13,14,15]; /* js对象里没有重复的key，所以可以通过以下方法来解决数组去重 ...
• u013415353
• 2016年07月03日 00:20
• 251

## 数组去重方法大全

• a214161398a
• 2016年05月15日 13:19
• 4116

## 面试：数组去重方法简单总结~

• qq_31164127
• 2017年04月27日 14:40
• 394

## 数组去重的四种方法

• fuckof
• 2016年08月19日 16:18
• 1571

## JS数组去重2017

• zhalcie2011
• 2017年06月06日 14:58
• 502

举报原因： 您举报文章：数组去重 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)