📌  相关文章
📜  计算数组javascript代码示例中重复对的数量

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

代码示例2
// how to count number of duplicate pairs in array javascript
function countNumberOfPairs(arr) {
    let pairsCount = 0;
    let counterObj = {};

  for (let num of arr) {
    if (!counterObj[num]) {
      counterObj[num] = 1;
    } else {
      counterObj[num] = counterObj[num] + 1;

      if (counterObj[num] === 2) {
        pairsCount++;
        counterObj[num] = 0;
      }
    }
  }
  return pairsCount;
}