Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

counts the duplicates in an array using for loop

function countDuplicates(arr) {
  const counts = {};
  for (let i = 0; i < arr.length; i++) {
    let element = arr[i];
    counts[element] = (counts[element] || 0) + 1;
  }
  return counts;
}
const arr = ["1", "5", "9", "14", "5", "22", "48", "25", "22", "20", "9", "13"];
console.log(countDuplicates(arr));
 
PREVIOUS NEXT
Tagged: #counts #duplicates #array #loop
ADD COMMENT
Topic
Name
4+8 =