Created
May 27, 2018 06:44
-
-
Save thmain/b307633e9353a452b61332c6b8a6923d to your computer and use it in GitHub Desktop.
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
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