how to find the duplicate values by given value in javascript code example
Example 1: how to get duplicate values from array in javascript
const arr = ["q", "w", "w", "e", "i", "u", "r"]
arr.reduce((acc, cur) => {
if(acc[cur]) {
acc.duplicates.push(cur)
} else {
acc[cur] = true //anything could go here
}
}, { duplicates: [] })
Example 2: counting duplicate values javascript
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });