TheAlgorithms/Java · error · IllegalArgumentException

No more than half the number of values may be the same.

Error message

No more than half the number of values may be the same.

What it means

A wiggle-sorted array needs strictly more non-median positions than median positions, otherwise there is no way to interleave the repeated median without two equal values becoming adjacent in a way that violates the <=/>= alternation. If the median value appears more than ceil(n/2) times, no valid arrangement exists, so WiggleSort throws IllegalArgumentException. This is the general (both odd and even length) duplicate-count guard, complementary to the odd-only check at line 74.

Source

Thrown at src/main/java/com/thealgorithms/sorts/WiggleSort.java:79

        int numMedians = 0;

        for (T sortThi : sortThis) {
            if (0 == sortThi.compareTo(median)) {
                numMedians++;
            }
        }
        // added condition preventing off-by-one errors for odd arrays.
        // https://cs.stackexchange.com/questions/150886/how-to-find-wiggle-sortable-arrays-did-i-misunderstand-john-l-s-answer?noredirect=1&lq=1
        if (sortThis.length % 2 == 1 && numMedians == ceil(sortThis.length / 2.0)) {
            T smallestValue = select(Arrays.asList(sortThis), 0);
            if (!(0 == smallestValue.compareTo(median))) {
                throw new IllegalArgumentException("For odd Arrays if the median appears ceil(n/2) times, "
                    + "the median has to be the smallest values in the array.");
            }
        }
        if (numMedians > ceil(sortThis.length / 2.0)) {
            throw new IllegalArgumentException("No more than half the number of values may be the same.");
        }

        triColorSort(sortThis, median);
        return sortThis;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Reduce the count of the dominant value below the ceil(n/2) threshold by introducing distinct values.
  2. Grow the array with distinct elements so no single value exceeds ceil(n/2) occurrences.
  3. Pre-screen inputs and reject/report over-represented data instead of attempting the sort.

Example fix

// before
Integer[] a = {1, 1, 1, 1, 2}; // median appears 4 > ceil(5/2)=3
WiggleSort.sort(a);            // throws

// after
Integer[] a = {1, 1, 1, 2, 3}; // median appears 3, equals threshold, ok
WiggleSort.sort(a);
Defensive patterns

Strategy: validation

Validate before calling

static <T extends Comparable<T>> boolean isWiggleSortable(T[] a) {
    Map<T,Long> freq = Arrays.stream(a)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    long limit = (long) Math.ceil(a.length / 2.0);
    return freq.values().stream().noneMatch(c -> c > limit);
}

Prevention

When it happens

Trigger: Calling the sort on any array where one value (the median) appears more than ceil(n/2) times. Example: {1,1,1,1,2} -> median=1 appears 4 > ceil(5/2)=3 -> throws. Also {2,2,2} triggers this branch (3 > ceil(3/2)=2).

Common situations: Heavily skewed or constant arrays; sensor/telemetry data with a dominant reading; test fixtures using repetitive values; configs that pass the same default repeatedly.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/27f5afdd9bef9df8. Report an issue: GitHub.