Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

count number in a list javascript

let arr = [1,1,1,2,2,3]

var count = new Map();
for(num in arr){
   if (count.size !== 0 && count.has(arr[num])){
          count = count.set(arr[num],1+ count.get(arr[num]))
          }
        else{
            count = count.set(arr[num],1);}
  
/* Output

Map(3) { 1 => 3, 2 => 2, 3 => 1 }
*/
     
Source by www.sitepoint.com #
 
PREVIOUS NEXT
Tagged: #count #number #list #javascript
ADD COMMENT
Topic
Name
2+5 =