Learn Sort Characters by Frequency with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice fundamentals and parsing.
INTERACTIVE EXECUTION STUDIO
LEARN BY DOING
Predict. Execute. Understand.
Follow the source code, inspect live state changes, and predict the next operation before revealing it.
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input
"tree"
Execution steps
Update frequencies
Emit most frequent remaining
Computed output
"eert"
JavaScript reference implementation
const solve = (a,s)=>{
let m=new Map;
for(let x of a){
m.set(x,(m.get(x)||0)+1);
s("Update frequencies",{
Counts:[...m].map(p=>p.join(":"))
})
}
let o="";
for(let [x,c] of [...m].sort((a,b)=>b[1]-a[1]||a[0].localeCompare(b[0]))){
o+=x.repeat(c);
s("Emit most frequent remaining",{
Output:[...o]
})
}
return o
};