Skip to content

数组去重

js
const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}];
// => [1, '1', 17, true, false, 'true', 'a', {}, {}]

方法一:利用Set

js
const res1 = Array.from(new Set(arr));

方法二:两层for循环+splice

js
const arr2 = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}];
const unique1 = arr => {
  let len = arr.length;
  for (let i = 0; i < len; i++) {
    for (let j = i + 1; j < len; j++) {
      if (arr[i] === arr[j]) {
        arr.splice(j, 1);// 每删除一个树,j--保证j的值经过自加后不变。同时,len--,减少循环次数提升性能        
        len--;
        j--;
      }
    }
  }
  return arr;
}

方法三:利用indexOf || include

js
// indexOf 
const unique2 = arr => {  
  const res = [];  
  for (let i = 0; i < arr.length; i++) {    
    if (res.indexOf(arr[i]) === -1) res.push(arr[i]);  
  }  
  return res;
}

// include
const unique3 = arr => {  
  const res = [];  
  for (let i = 0; i < arr.length; i++) {    
    if (!res.includes(arr[i])) res.push(arr[i]);  
  }  
  return res;
}

方法四:利用Map

js
const unique4 = arr => {  
  const map = new Map();  
  const res = [];  
  for (let i = 0; i < arr.length; i++) {    
    if (!map.has(arr[i])) {      
      map.set(arr[i], true);
      res.push(arr[i]);    
    }  
  }  
  return res;
}