# JS数组去重常见方法分析

## 一、简单的去重方法

``````// 简单数组去重法

function unique(arr){
var temp = [];
for(var i = 0; i < arr.length; i++){
if(temp.indexOf(arr[i]) == -1){
temp.push(arr[i]);
}
}
return temp;
}

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));

``````

``````[1, 2, 3, 5, 9]

["a", "c", "e", "f"]

[1, 2, "2", "a", "c", 3, "e", 5, 9, "f"]

[1, 2, "2", "a", "c" 3, {k:123}, "e", 5, 9, [1,2,3], "f", [1,2,3], [1,"2",3], [4,5,6], {k:123}, {k:"123"}, {f:234} ]
``````

## 二、排序相邻去重法

``````//排序相邻去重法

function unique(arr){
arr.sort();
var temp=[arr[0]];
for(var i = 1; i < arr.length; i++){
if( arr[i] !== temp[temp.length-1]){
temp.push(arr[i]);
}
}
return temp;
}

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));

``````

``````[1, 2, 3, 5, 9]

["a", "c", "e", "f"]

[1, 2, "2", 2, "2", 3, 5, 9, "a", "c", "e", "f"]

[1, [1, 2, 3], [1, 2, 3], [1, "2", 3], 2, "2", 2, "2", 3, [4, 5, 6], 5, 9, {k: 123}, {k: 123}, {k: "123"}, {f: 234}, "a", "c", "e", "f"]
``````

## 三、数组下标法

``````//数组下标法

function unique(arr){
var temp = [];
for(var i = 0; i < arr.length; i++) {
if(arr.indexOf(arr[i]) == i){
temp.push(arr[i])
}
}
return temp;
}

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));
``````

``````[1, 2, 3, 5, 9]

["a", "c", "e", "f"]

[1, 2, "2", "a", "c", 3, "e", 5, 9, "f"]

[1, 2, "2", "a", "c", 3, {k: 123}, "e", 5, 9, [1, 2, 3], "f", [1, 2, 3], [1, "2", 3], [4, 5, 6], {k: 123}, {k: "123"}, {f: 234}]
``````

## 四、对象键值去重法

``````//对象键值去重法

function unique(arr){
var temp = {}, result = [], len = arr.length, val, type;
for (var i = 0; i < len; i++) {
val = arr[i];
type = typeof val;
if (!temp[val]) {
temp[val] = [type];
result.push(val);
} else if (temp[val].indexOf(type) < 0) {
temp[val].push(type);
result.push(val);
}
}
return result;
}

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));
``````

``````[1, 2, 3, 5, 9]

["a", "c", "e", "f"]

[1, 2, "2", "a", "c", 3, "e", 5, 9, "f"]

[1, 2, "2", "a", "c", 3, {k: 123}, "e", 5, 9, [1, 2, 3], "f", [4, 5, 6]]
``````

``````function unique(arr){
var temp = {}, result = [], len = arr.length, val, type;
for (var i = 0; i < len; i++) {
val = arr[i];
type = typeof val;

if(type == "object"){
result.push(val);
}else if (!temp[val]) {
temp[val] = [type];
result.push(val);
} else if (temp[val].indexOf(type) < 0) {
temp[val].push(type);
result.push(val);
}
}
return result;
}
``````

## 五、优化遍历数组法

``````//优化遍历数组法

function unique(arr){
var temp = [];
var index = [];
var l = arr.length;
for(var i = 0; i < l; i++) {
for(var j = i + 1; j < l; j++){
if (arr[i] === arr[j]){
i++;
j = i;
}
}
temp.push(arr[i]);
index.push(i);
}

return temp;
}

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));
``````

``````[1, 2, 3, 5, 9]

["e", "a", "c", "f"]

[1, 2, "2", "e", 3, 5, "a", 9, "c", "f"]

[1, 2, "2", {k:123}, "e", 3, 5, "a", 9, [1,2,3], "c", "f", [1,2,3], [1,"2",3], [4,5,6], {k:123}, {k:"123"}, {f:234} ]
``````

## 六、 Set去重法

ES6 提供了新的数据结构 Set。它类似于数组，但是成员的值都是唯一的，没有重复的值。 其用于数组去重的代码如下所示：

``````//Set去重法

const unique = arr => [...new Set(arr)];

var arr1 = [1,2,2,3,5,3,5,9];

console.log(unique(arr1));

var arr2 = ['a','c','e','a','c','f'];

console.log(unique(arr2));

var arr3 = [1,2,"2",2,'a','c',3,'2','e',5,3,5,'a',9,'c','f'];

console.log(unique(arr3));

var arr4 = [1,2,"2",2,'a','c',3,'2',{k:123},'e',5,3,5,'a',9,[1,2,3],'c','f',[1,2,3],[1,'2',3],[4,5,6],{k:123},{k:'123'},{f:234}];

console.log(unique(arr4));

``````

``````[1, 2, 3, 5, 9]

["a", "c", "e", "f"]

[1, 2, "2", "a", "c", 3, "e", 5, 9, "f"]

[1, 2, "2", "a", "c", 3, {k: 123}, "e", 5, 9, [1, 2, 3], "f", [1, 2, 3], [1, "2", 3], [4, 5, 6], {k: 123}, {k: "123"}, {f: 234}]
``````