标签:fun lang indexof === 数组 you 去重 class har
// 演示代码
function unique(arr) {
/* your code */
}
let strings = ["Hare", "Krishna", "Hare", "Krishna",
"Krishna", "Krishna", "Hare", "Hare", ":-O"
];
alert( unique(strings) ); // Hare, Krishna, :-O
function unique(arr) {
return [...new Set(arr)];
}
function unique(arr) {
let hash = [];
for (let i = 0; i < arr.length; i++) {
if (hash.indexOf(arr[i]) === -1) {
hash.push(arr[i]);
}
}
return hash;
}
function unique(arr) {
arr.sort();
let hash = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[i + 1]) {
hash.push(arr[i]);
}
}
return hash;
}
标签:fun lang indexof === 数组 you 去重 class har
原文地址:https://www.cnblogs.com/Lotus3904/p/14307750.html