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
{
"a": [
1,
1,
1,
2,
2,
3
],
"k": 2
}
Execution steps
Count occurrence
Select top frequencies
Computed output
[
1,
2
]
JavaScript reference implementation
const solve = ({
a,k
},s)=>{
let m=new Map;
for(let x of a){
m.set(x,(m.get(x)||0)+1);
s("Count occurrence",{
Frequencies:[...m].map(p=>p.join(":"))
})
}
let o=[...m].sort((a,b)=>b[1]-a[1]).slice(0,k).map(x=>x[0]);
s("Select top frequencies",{
Output:o
});
return o
};