Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

count number of duplicate pairs in array javascript

// 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;
}
 
PREVIOUS NEXT
Tagged: #count #number #duplicate #pairs #array #javascript
ADD COMMENT
Topic
Name
6+9 =