Daily-Question icon indicating copy to clipboard operation
Daily-Question copied to clipboard

【Q445】实现一个数组去重函数 unique

Open shfshanyue opened this issue 4 years ago • 5 comments

shfshanyue avatar Dec 18 '20 03:12 shfshanyue

function unique(arr) {
  if (!Array.isArray(arr)) throw new TypeError();
  return [...new Set(arr)];
}

joyz0 avatar Jan 15 '21 08:01 joyz0

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
}

HuiFeiYa avatar Mar 09 '21 10:03 HuiFeiYa

const unique = list => [...new Set(list)]

shfshanyue avatar May 27 '21 14:05 shfshanyue

function unique(array) {
  return array.filter((item, index) => array.indexOf(item) === index);
}

haotie1990 avatar Jul 26 '21 08:07 haotie1990

function unique(arr) {
  return arr.reduce((acc, item) => (acc.includes(item) ? acc : acc.concat(item)), []);
}

Vi-jay avatar May 14 '22 04:05 Vi-jay