javascript array count duplicates 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: javascript check if array has duplicates

function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}

Example 3: counting duplicate values javascript

var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });

Tags:

Java Example