TheAlgorithms/Java · error · IllegalArgumentException
Input array should have at least 2 distinct elements
Error message
Input array should have at least 2 distinct elements
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/SecondMinMax.java:57
*/
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");
}
}
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int second = SecondMinMax.findSecondMin(arr);
// after
long distinct = Arrays.stream(arr).distinct().count();
if (distinct < 2) {
throw new IllegalStateException("Need >= 2 distinct values, found " + distinct);
}
int second = SecondMinMax.findSecondMin(arr); Defensive patterns
Strategy: validation
Validate before calling
long distinct = Arrays.stream(arr).distinct().count();
if (distinct < 2) {
throw new IllegalStateException("Need >= 2 distinct values");
}
int s = SecondMinMax.findSecondMin(arr); Try / catch
try {
int s = SecondMinMax.findSecondMin(arr);
} catch (IllegalArgumentException e) {
// all-equal is a valid state: return the single value or empty
return arr.length > 0 ? OptionalInt.of(arr[0]) : OptionalInt.empty();
} Prevention
- For 'second-best' queries, check distinct count, not just length.
- Recognize constant-value datasets as a legitimate edge case requiring an explicit policy.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- k must be between 1 and the size of the array
- Array must be non-empty.
- Array must be non-empty.
- Array must be non-empty.
- array must be non-empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/648e06ea4ceee17e.
Report an issue: GitHub.