Workspace Sort Characters By Frequency
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Sort Characters by Frequency visualizer

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.

1 / 8Execution step
Reference example · JavaScript
Inspect reference input
"tree"

Algorithm source

JavaScript reference implementation · highlighted trace statement
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
};

Execution canvas

Active Changed
STEP 1

Update frequencies

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Counts1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Strings · Fundamentals and parsing

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

  1. Update frequencies
  2. 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
};

Continue learning

Practice related DSA questions in the same topic.