This is the given array

```
let arr=[1,2,4,7,7,5]
```

Declare the two variables

```
let firstMax;
let secondMax;
```

Then find out the which is greater in the first 2 index values

```
if(arr[0] > arr[1]){
firstMax = arr[0]
}else{
secondMax=arr[1]
}
if(arr[1] > arr[0]){
firstMax = arr[1]
}else{
secondMax=arr[0]
}
```

Then begin the loop from 2 to last index of the array

## In the first loop

## In the second loop

## In the third loop

## In the fourth loop

Complete solution to find the second largest in the array

```
let arr=[1,2,4,7,7,5]
let firstMax;
let secondMax;
if(arr[0] > arr[1]){
firstMax = arr[0]
}else{
secondMax=arr[1]
}
if(arr[1] > arr[0]){
firstMax = arr[1]
}else{
secondMax=arr[0]
}
for(let i=2;i<arr.length;i++){
if(arr[i] > firstMax){
secondMax=firstMax;
firstMax=arr[i];
}
if(arr[i] > secondMax && arr[i] < firstMax){
secondMax = arr[i]
}
}
console.log("firstMax",firstMax)
console.log("secondMax",secondMax)
```

## Top comments (0)