count the number of duplicates javascript code example
Example 1: find duplicates and their count in an array javascript
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
Example 2: how to count duplicates in an array javascript
arr.reduce((b,c)=>((b[b.findIndex(d=>d.el===c)]||b[b.push({el:c,count:0})-1]).count++,b),[]);
Example 3: how to get duplicate values from array in javascript
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
const count = names =>
names.reduce((a, b) => ({ ...a,
[b]: (a[b] || 0) + 1
}), {})
const duplicates = dict =>
Object.keys(dict).filter((a) => dict[a] > 1)
console.log(count(names))
console.log(duplicates(count(names)))