Back to questionsDSA Visualizers: 388 Coding Questions Second Largest Element visualizer Learn Second Largest Element with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice fundamentals.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 5 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
5,
2,
5,
3
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let f=-Infinity,b=-Infinity;
3 a.forEach((x,i)=>{
4 if (x>f){
5 b=f;
6 f=x
7 }else if (x<f&&x>b)b=x;
8 s("Maintain distinct maxima",{
9 Array:a,Max:[f],Second:[Number.isFinite(b)?b:null]
10 },[i])
11 });
12 return b
13 };
Execution canvas Active ChangedSTEP 1 Maintain distinct maxima Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Arrays · Fundamentals
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 [
5,
2,
5,
3
] Execution steps Maintain distinct maxima Computed output 3 JavaScript reference implementation const solve = (a,s)=>{
let f=-Infinity,b=-Infinity;
a.forEach((x,i)=>{
if(x>f){
b=f;
f=x
}else if(x<f&&x>b)b=x;
s("Maintain distinct maxima",{
Array:a,Max:[f],Second:[Number.isFinite(b)?b:null]
},[i])
});
return b
};Continue learning Practice related DSA questions in the same topic.