Created
September 24, 2018 10:52
-
-
Save abhinavnigam2207/13632f60c35b26119a2c67874c4a4e23 to your computer and use it in GitHub Desktop.
Find second largest In an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findSecondLargest(arr) { | |
let max = arr[0]; | |
let secondMax = arr[0]; | |
arr.forEach((elem) => { | |
if(elem > max) { | |
secondMax = max; | |
max = elem; | |
} else if(elem<max && elem>secondMax) { | |
secondMax = elem; | |
} | |
}); | |
return secondMax; | |
} | |
const arr = [2,17,4,7,9,42,33,3,7]; | |
findSecondLargest(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment