TheAlgorithms/Java · error · IllegalArgumentException

Input x-coordinates must be unique.

Error message

Input x-coordinates must be unique.

What it means

Thrown by Neville.interpolate when the x-coordinates array contains duplicate values. Neville's interpolation algorithm divides by (x[i] - x[i+k]), so duplicate x-coordinates would cause division by zero. The library defensively checks uniqueness upfront via a HashSet rather than allowing a silent ArithmeticException or NaN result downstream.

Source

Thrown at src/main/java/com/thealgorithms/maths/Neville.java:45

     * @param y The y-coordinates of the points. Must be the same length as x.
     * @param target The x-coordinate at which to evaluate the polynomial.
     * @return The interpolated y-value at the target x-coordinate.
     * @throws IllegalArgumentException if the lengths of x and y arrays are
     * different, if the arrays are empty, or if x-coordinates are not unique.
     */
    public static double interpolate(double[] x, double[] y, double target) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("x and y arrays must have the same length.");
        }
        if (x.length == 0) {
            throw new IllegalArgumentException("Input arrays cannot be empty.");
        }

        // Check for duplicate x-coordinates to prevent division by zero
        Set<Double> seenX = new HashSet<>();
        for (double val : x) {
            if (!seenX.add(val)) {
                throw new IllegalArgumentException("Input x-coordinates must be unique.");
            }
        }

        int n = x.length;
        double[] p = new double[n];
        System.arraycopy(y, 0, p, 0, n); // Initialize p with y values

        for (int k = 1; k < n; k++) {
            for (int i = 0; i < n - k; i++) {
                p[i] = ((target - x[i + k]) * p[i] + (x[i] - target) * p[i + 1]) / (x[i] - x[i + k]);
            }
        }

        return p[0];
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Inspect the x array for duplicates before calling: deduplicate or remap so all x values are distinct.
  2. If duplicate x values are legitimate (repeated measurements), aggregate y values (e.g., average) at each distinct x before interpolating.
  3. If the duplicates come from floating-point rounding, perturb one value or use a tolerance-aware uniqueness check before passing to interpolate.

Example fix

// before
double[] x = {1.0, 2.0, 2.0, 4.0};
double[] y = {0.0, 0.69, 0.70, 1.39};
double r = Neville.interpolate(x, y, 3.0);

// after — collapse duplicate x by averaging y
double[] x = {1.0, 2.0, 4.0};
double[] y = {0.0, 0.695, 1.39};
double r = Neville.interpolate(x, y, 3.0);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean allXUnique(double[] x) {
    Set<Double> seen = new HashSet<>();
    for (double v : x) {
        if (!seen.add(v)) return false;
    }
    return true;
}

// before calling:
if (!allXUnique(x)) {
    throw new IllegalArgumentException("x has duplicate coordinates");
}
double r = Neville.interpolate(x, y, target);

Prevention

When it happens

Trigger: Calling interpolate(double[] x, double[] y, double target) where x[] contains at least two equal double values (e.g., x = {1.0, 2.0, 2.0, 4.0}). Floating-point values that are bit-identical (including +0.0 vs -0.0 distinction per HashSet.equals) trigger it.

Common situations: Data sampled from a sensor or dataset where two samples share the same timestamp; copy-paste errors in hand-entered coordinate arrays; merging datasets that produce repeated abscissa values; using integer-valued x arrays (0,1,1,2) where rounding collapsed distinct floats.

Related errors


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