Error Code Summary
JavaScript
Hard
4 views
Problem Description
Input has n lines. Some lines look like ERROR:CODE where CODE is a word. Count each code and print in descending count, tie by code. Ignore lines that do not match.
Input Format
Line1: n. Next n lines text.
Output Format
Multiple lines: CODE count.
Sample Test Case
Input:
6
ERROR:DB
hello
ERROR:DB
ERROR:AUTH
ERROR:AUTH
ERROR:DB
Official Solution
const fs=require('fs');const lines=fs.readFileSync(0,'utf8').split(/\
?\
/);const n=Number((lines[0]||'').trim());if(!n)process.exit(0);const map=new Map();for(let i=1;i<=n;i++){const s=(lines[i]||'').trim();const m=/^ERROR:([A-Za-z0-9_]+)$/.exec(s);if(!m)continue;const code=m[1];map.set(code,(map.get(code)||0)+1);}const arr=[...map.entries()];arr.sort((a,b)=>b[1]-a[1]||a[0].localeCompare(b[0]));process.stdout.write(arr.map(([c,k])=>c+' '+k).join('\
'));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!