Skip to content

Instantly share code, notes, and snippets.

@nwbkhan
Created January 17, 2020 16:09
Show Gist options
  • Save nwbkhan/105cea431f56056fe2dee9e411c5ab4e to your computer and use it in GitHub Desktop.
Save nwbkhan/105cea431f56056fe2dee9e411c5ab4e to your computer and use it in GitHub Desktop.
find 2nd largest number fron numbers[...]
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Integer i[] = new Integer[]{321, 312, 34, 43, 5, 667, 657, 4, 234, 5, 564, 6};
Integer great_no;
Integer great_no2;
Integer len = i.length;
if (len < 2) {
System.out.println("jdaskjdkas");
return;
}
great_no = i[1];
great_no2 = i[1];
for (int j = 1; j < len; j++) {
if (i[j] > great_no) {
Integer temp = great_no;
great_no = i[j];
great_no2 = temp;
} else {
if (i[j] > great_no2) {
System.out.println("caught- ");
great_no2 = i[j];
}
}
}
// would not work for 2 no
// add check for 2no
System.out.println("2 greater - " + great_no2);
System.out.println("greater - " + great_no);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment