Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

count duplicate elements in array javascript

const counts = {};
const sampleArray = ["1", "5", "9", "14", "5", "22", "48", "25", "22", "20", "9" ,"13"]
sampleArray.forEach(function (x) { counts[x] = (counts[x] || 0) + 1; });
console.log(counts)
 // output: {
  '1': 1,
  '5': 2,
  '9': 2,
  '13': 1,
  '14': 1,
  '20': 1,
  '22': 2,
  '25': 1,
  '48': 1
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #count #duplicate #elements #array #javascript
ADD COMMENT
Topic
Name
5+9 =