📌  相关文章
📜  如何在字符串中查找重复项 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:53.159000             🧑  作者: Mango

代码示例1
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'];

const count = names =>
  names.reduce((a, b) => {
    a[b] = (a[b] || 0) + 1;
    return a;
  }, {}); // don't forget to initialize the accumulator
const duplicates = dict => Object.keys(dict).filter(a => dict[a] > 1);

console.log(count(names)); // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))); // [ 'Nancy' ]