Second Max of Array

Question

Find the second max number in a given array.

Notice
You can assume the array contains at least two numbers.

Example
Given [1, 3, 2, 4] , return 3 .
Given [1, 2] , return 1 .

Thinking

  • Max value can be duplicate values

Solution

Java

public class Solution {
    /**
     * @param nums: An integer array.
     * @return: The second max number in the array.
     */
    public int secondMax(int[] nums) {
        /* your code */
        if (nums == null || nums.length < 2) {
            // Input error. Throw an exception
            // throw new Exception("At least 2 numbers in the array")
        }
        int max = Math.max(nums[0], nums[1]);
        int second = Math.min(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] >= max) {
                second = max;
                max = nums[i];
            }
        }
        return second;
    }
}