Basic Type Buckets
JavaScript
Easy
3 views
Problem Description
You get a JSON array with mixed values. Count how many are: number, string, boolean, null, array, object. Print 6 counts in this order.
Input Format
One line JSON array.
Output Format
Six integers space separated.
Sample Test Case
Input:
[1,"a",true,null,[2],{"x":1},false]
Constraints
Array length up to 2e5.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const arr=JSON.parse(s);let num=0,str=0,boo=0,nul=0,ary=0,obj=0;for(const v of arr){if(v===null)nul++;else if(Array.isArray(v))ary++;else{const t=typeof v;if(t==='number')num++;else if(t==='string')str++;else if(t==='boolean')boo++;else if(t==='object')obj++;}}process.stdout.write([num,str,boo,nul,ary,obj].join(' '));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!