DEV Community

Anjali Gurjar
Anjali Gurjar

Posted on

MAx second and first Number

const arr = [-1, -2, -3, 4, 5, 7, 8, 4];

let second =-Infinity
let max=-Infinity
let result = [];

for( let num of arr){
if(num>max){
second=max
max=num
}
else if(num>second && num<num){
second=num
}

}
console.log(second)
console.log(max)

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post