let arr = [1, 1, 2, 2, 6, 7, 7, 5, 5, 8, 8, 2, 3, 9, 3, 2, 9, 4];
// Array.from + Set 去重
let arr1 = Array.from(new Set(arr));
console.log('arr1', arr1)
// includes + push 去重
let arr2 = [];
arr.forEach(ele => {
if (!arr2.includes(ele)) {
arr2.push(ele);
}
})
console.log('arr2', arr2)
// indexOf + push 去重
let arr3 = [];
arr.forEach(ele => {
if (arr3.indexOf(ele) == -1) {
arr3.push(ele);
}
})
console.log('arr3', arr3)
// sort + push 去重
let arr4 = [];
arrA = arr.map((el) => el).sort((a, b) => a - b);
arrA.forEach((ele, index) => {
if (arrA[index] !== arrA[index + 1]) {
arr4.push(ele)
}
})
console.log('arr4', arr4)
// filter + indexOf 去重
let arr5 = [];
arr5 = arr.filter((ele, index) => {
return arr.indexOf(ele) == index
})
console.log('arr5', arr5)
// reduce + includes + push 去重
let arr6 = arr.reduce((pre, item) => {
if (!pre.includes(item)) {
pre.push(item)
}
return pre
}, [])
console.log('arr6', arr6)
// reduce + indexOf + push 去重
let arr7 = arr.reduce((pre, item) => {
if (pre.indexOf(item) == -1) {
pre.push(item)
}
return pre
}, [])
console.log('arr7', arr7)
// 双 for + splice 去重
let arr8 = arr.map((el) => el);
for (let i = 0; i < arr8.length; i++) {
for (let j = i + 1; j < arr8.length; j++) {
if (arr8[i] == arr8[j]) {
arr8.splice(j, 1);
j--;
}
}
}
console.log('arr8', arr8)
console