Tuesday, 19 July 2011

Second Biggest Number

public class SecondMaximum {

    public static void main(String arg[]) throws Exception {
        int[] numbers = {87, 900, 500, 400, 700, 600, 300, 200,};
       
        int max = getMaxValue(numbers);
        System.out.println("Second Max number : "+ max);
    }

    public static int getMaxValue(int[] numbers) {
        int firstMax = numbers[0];
        int secondMax = 0;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > firstMax) {
                firstMax = numbers[i];
            }
            else if(numbers[i] > secondMax && numbers[i] != firstMax){
                secondMax = numbers[i];
            }
        }
        return secondMax;
    }
}

1 comment: