JavaScript
常见代码片段
Deduplication

去重

const arr = [1, 2, 3, 1, 4];
// 方法1
const deduped1 = [...new Set(arr)];
 
// 方法2
const deduped2 = arr.filter((item, index) => arr.indexOf(item) === index);
 
// 方法3
const deduped3 = arr.reduce((acc, cur) => {
  if (!acc.includes(cur)) {
    acc.push(cur);
  }
  return acc;
}, []);
 
console.log(deduped1); // [1, 2, 3, 4]
console.log(deduped2); // [1, 2, 3, 4]
console.log(deduped3); // [1, 2, 3, 4]