实现一个数组去重函数 unique
Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 453 (opens in a new tab)
Author 回答者: joyz0 (opens in a new tab)
function unique(arr) {
if (!Array.isArray(arr)) throw new TypeError();
return [...new Set(arr)];
}
Author 回答者: HuiFeiYa (opens in a new tab)
function unique(arr){
const map = new Map()
arr.forEach(value=>{
map.set(value,value)
})
const list = []
for (let key of map.keys()) {
list.push(key)
}
return list
}
Author 回答者: shfshanyue (opens in a new tab)
const unique = (list) => [...new Set(list)];
Author 回答者: haotie1990 (opens in a new tab)
function unique(array) {
return array.filter((item, index) => array.indexOf(item) === index);
}
Author 回答者: Vi-jay (opens in a new tab)
function unique(arr) {
return arr.reduce(
(acc, item) => (acc.includes(item) ? acc : acc.concat(item)),
[],
);
}