经典面试题----数组去重

145 阅读1分钟

方法一(不使用Set)

unique = (array) => {
    const hash = []
    for(let i=0;i<array.length; i++){
        hash[array[i]] = true
    }
    const result = []
    for(let j in hash){
        result.push(j)
    }
    return result
}

缺点:只支持数字或者字符串数组,如果数组里面有对象,比如array = [{number:1}, 2],就会出错

方法二(使用Set)

unique = (array) => {
    return [...new Set(array)] 
    // 或者 return Array.from(new Set(array))
}

缺点:API 太新,旧浏览器不支持

方法三(使用Map)

unique = (array) => {
  let map = new Map();
  let result = []
  for (let i = 0; i < array.length; i++) {
    if(map.has(array[i])) { // 判断 map 中是否已有该 key 
      continue
    } else {  // 如果 map 中没有该 key,就加入 result 中
      map.set(array[i], true);  
      result.push(array[i]);
    }
  } 
  return result;
}

缺点:API 太新,旧浏览器不支持