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

Code before:
package com.thealgorithms.maths;

import java.util.function.BiPredicate;

public final class SecondMinMax {

    /**
     * Utility class for finding second maximum or minimum based on BiPredicate
     * @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 )
     */

    private SecondMinMax() {
    }

    private static int secondBest(final int[] arr, final int initialVal, final BiPredicate<Integer, Integer> isBetter) {
        checkInput(arr);
        int best = initialVal, 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) {

Review comment (suggestion):
```suggestion
        int best = initialVal;
        int secBest = initialVal;
```

Code after:
package com.thealgorithms.maths;

import java.util.function.BiPredicate;

public final class SecondMinMax {

    /**
     * Utility class for finding second maximum or minimum based on BiPredicate
     * @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 )
     */

    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);
    }