Skip to content

Instantly share code, notes, and snippets.

@SumitJainUTD
Created May 10, 2015 04:48
Show Gist options
  • Save SumitJainUTD/96445db5b06952463fc5 to your computer and use it in GitHub Desktop.
Save SumitJainUTD/96445db5b06952463fc5 to your computer and use it in GitHub Desktop.
public class SecondLargestElement {
public static int findSecond(int[] A) {
int fstNo = A[0];
int sndNo = -1;
for (int i = 1; i < A.length; i++) {
if (fstNo < A[i]) {
sndNo = fstNo;// store the first largest no value to second
// largest
fstNo = A[i];
} else if (sndNo < A[i]) {
sndNo = A[i];
}
}
return sndNo;
}
public static void main(String[] args) {
int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 };
System.out.println("Second largest Element : " + findSecond(A));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment