duplicate in array javascript code example
Example 1: count duplicates array js
uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
var count = {};
uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;});
console.log(count);
Example 2: check for duplicates in array javascript
[1, 2, 3].every((e, i, a) => a.indexOf(e) === i)
[1, 2, 1].every((e, i, a) => a.indexOf(e) === i)
Example 3: 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
}
}, { duplicates: [] })