{"record":{"id":"648e06ea4ceee17e","repo":"TheAlgorithms/Java","slug":"input-array-should-have-at-least-2-distinct-elemen","errorCode":null,"errorMessage":"Input array should have at least 2 distinct elements","messagePattern":"Input array should have at least 2 distinct elements","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/SecondMinMax.java","lineNumber":57,"sourceCode":"     */\n\n    public static int findSecondMin(final int[] arr) {\n        return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);\n    }\n\n    public static int findSecondMax(final int[] arr) {\n        return secondBest(arr, Integer.MIN_VALUE, (a, b) -> a > b);\n    }\n\n    private static void checkInput(final int[] arr) {\n        if (arr.length < 2) {\n            throw new IllegalArgumentException(\"Input array must have length of at least two\");\n        }\n    }\n\n    private static void checkOutput(final int secNum, final int initialVal) {\n        if (secNum == initialVal) {\n            throw new IllegalArgumentException(\"Input array should have at least 2 distinct elements\");\n        }\n    }\n}\n","sourceCodeStart":39,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/SecondMinMax.java#L39-L61","documentation":"Thrown by SecondMinMax (via private checkOutput) when the computed second-best still equals its initial sentinel (Integer.MAX_VALUE for min, Integer.MIN_VALUE for max). That happens only when no distinct second value exists, e.g., all elements are identical, because the second-best variable is never updated away from its initializer.","triggerScenarios":"Call findSecondMin(new int[]{7,7,7}) or findSecondMax(new int[]{3,3}). Also triggered by an array of identical values, or an array whose distinct-element count is 1.","commonSituations":"Degenerate datasets where every sample is equal (constant sensor reading, single-value lookup table, a column of repeated defaults), or unit tests with simplistic fixtures.","solutions":["Pre-filter the array to distinct values (e.g., via a Set) and verify at least 2 distinct elements remain before calling.","Treat 'all values equal' as a valid business outcome and wrap the call, returning the single value or an Optional.empty().","If duplicates are data errors, fix the upstream producer."],"exampleFix":"// before\nint second = SecondMinMax.findSecondMin(arr);\n\n// after\nlong distinct = Arrays.stream(arr).distinct().count();\nif (distinct < 2) {\n    throw new IllegalStateException(\"Need >= 2 distinct values, found \" + distinct);\n}\nint second = SecondMinMax.findSecondMin(arr);","handlingStrategy":"validation","validationCode":"long distinct = Arrays.stream(arr).distinct().count();\nif (distinct < 2) {\n    throw new IllegalStateException(\"Need >= 2 distinct values\");\n}\nint s = SecondMinMax.findSecondMin(arr);","typeGuard":null,"tryCatchPattern":"try {\n    int s = SecondMinMax.findSecondMin(arr);\n} catch (IllegalArgumentException e) {\n    // all-equal is a valid state: return the single value or empty\n    return arr.length > 0 ? OptionalInt.of(arr[0]) : OptionalInt.empty();\n}","preventionTips":["For 'second-best' queries, check distinct count, not just length.","Recognize constant-value datasets as a legitimate edge case requiring an explicit policy."],"tags":["math","array","distinct-values","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}