Find the second largest number in the array
I have written the following code for finding the 1st largest number in the given array and please do correct me in case there is some error in the program and can any one help me in finding the 2nd largest number in the array.
The code which i have written is as follows:
public class Assignment1 {
public static void main(String args[]) {
int i;
int temp = 95;
int arr[] = {95, 20, 34, 100, 9,150};
for (i = 0; i < arr.length - 1; i++) {
if (i == 0) {
temp = arr[i];
} else if (temp < arr[i]) {
temp = arr[i];
}
}
System.out.println("Largest is :" + temp);
}
}