Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/SecondMinMax.java (Java)

Code before:

    public static int findSecondMin(int[] arr) {
        int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
        if (arr.length == 0)
            throw new IllegalArgumentException("Cant find Second Minimum for empty array");
        else if (arr.length == 1)
            throw new IllegalArgumentException("Cant find Second Minimum for single element");
        else
            for (int num : arr)
                if (num < min) {
                    secMin = min;
                    min = num;
                } else if ((num < secMin) && (num != min))
                    secMin = num;
        if (secMin == Integer.MAX_VALUE) throw new IllegalArgumentException("Cant find Second Minimum in array full of same numbers");
        return secMin;
    }

    public static int findSecondMax(int[] arr) {
        int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE;
        if (arr.length == 0)
            throw new IllegalArgumentException("Cant find Second Maximum for empty array");
        else if (arr.length == 1)
            throw new IllegalArgumentException("Cant find Second Maximum for single element");
        else
            for (int num : arr)
                if (num > max) {
                    secMax = max;
                    max = num;
                } else if ((num > secMax) && (num != max))
                    secMax = num;
        if (secMax == Integer.MIN_VALUE) throw new IllegalArgumentException("Cant find Second Maximum in array full of same numbers");
        return secMax;
    }
}


Review comment (suggestion):
```suggestion
            for (final int num : arr)
```

Code after:
     */

    private SecondMinMax() {
    }

    private static int secondBest(final int[] arr, final int initialVal, final BiPredicate<Integer, Integer> isBetter) {
        checkInput(arr);
        int best = initialVal;
        int secBest = initialVal;
        for (final int num : arr) {
            if (isBetter.test(num, best)) {
                secBest = best;
                best = num;
            } else if ((isBetter.test(num, secBest)) && (num != best)) {
                secBest = num;
            }
        }
        checkOutput(secBest, initialVal);
        return secBest;
    }

    /**
     * @brief Finds the Second minimum / maximum value from the array
     * @param arr the input array
     * @exception IllegalArgumentException => if input array is of length less than 2 also if all elements are same
     * @return the second minimum / maximum value from the input array
     * @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )
     */

    public static int findSecondMin(final int[] arr) {
        return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);
    }

    public static int findSecondMax(final int[] arr) {
        return secondBest(arr, Integer.MIN_VALUE, (a, b) -> a > b);
    }

    private static void checkInput(final int[] arr) {
        if (arr.length < 2) {
            throw new IllegalArgumentException("Input array must have length of at least two");
        }
    }

    private static void checkOutput(final int secNum, final int initialVal) {
        if (secNum == initialVal) {
            throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
        }
    }
}