Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Created September 24, 2018 10:52
Show Gist options
  • Save abhinavnigam2207/13632f60c35b26119a2c67874c4a4e23 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/13632f60c35b26119a2c67874c4a4e23 to your computer and use it in GitHub Desktop.
Find second largest In an array
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