极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。
山月训练营之面试直通车 服务上线了,从准备简历、八股文准备、项目经历准备、面试、面经、面经解答、主观问题答复、谈薪再到入职的一条龙服务。

# 实现一个数组去重函数 unique

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 453 (opens new window)

Author

回答者: justable (opens new window)

function unique(arr) {
  if (!Array.isArray(arr)) throw new TypeError();
  return [...new Set(arr)];
}
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
}
const unique = (list) => [...new Set(list)];
function unique(array) {
  return array.filter((item, index) => array.indexOf(item) === index);
}

Author

回答者: Vi-jay (opens new window)

function unique(arr) {
  return arr.reduce(
    (acc, item) => (acc.includes(item) ? acc : acc.concat(item)),
    []
  );
}
Last Updated: 6/26/2022, 10:48:10 AM