Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

count duplicate array javascript

const months = ['april','may','june','may','may','june'];

const countDuplicates = months.reduce((obj,month)=>{
    if(obj[month] == undefined){
        obj[month] = 1;
        return obj;
    }else{
        obj[month]++;
        return obj;
    }
},{});
console.log(countDuplicates);//output:{april: 1, may: 3, june: 2}
Source by gist.github.com #
 
PREVIOUS NEXT
Tagged: #count #duplicate #array #javascript
ADD COMMENT
Topic
Name
1+5 =