Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 455 Bytes

difference.md

File metadata and controls

20 lines (16 loc) · 455 Bytes
方法名 标签
difference
数组,初级

计算两个数组的差异,而不过滤重复的值。

  • 根据 b 创建 Set 集合来获取 b 中的唯一值。
  • a 使用 Array.prototype.filter() 来保留不在 b 中的值,以及使用 Set.prototype.has()
const difference = (a, b) => {
  const s = new Set(b);
  return a.filter(x => !s.has(x));
};
difference([1, 2, 3, 3], [1, 2, 4]); // [3, 3]