标签:new 数组 lan 代码 sort class 数组去重 unique js数组
// 演示代码
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;
}
标签:new 数组 lan 代码 sort class 数组去重 unique js数组
原文地址:https://www.cnblogs.com/Lotus3904/p/14313800.html