Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 27, 2018 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/b307633e9353a452b61332c6b8a6923d to your computer and use it in GitHub Desktop.
Save thmain/b307633e9353a452b61332c6b8a6923d 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