{"record":{"id":"fed70e582ed73c34","repo":"TheAlgorithms/Java","slug":"input-x-coordinates-must-be-unique","errorCode":null,"errorMessage":"Input x-coordinates must be unique.","messagePattern":"Input x-coordinates must be unique\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Neville.java","lineNumber":45,"sourceCode":"     * @param y The y-coordinates of the points. Must be the same length as x.\n     * @param target The x-coordinate at which to evaluate the polynomial.\n     * @return The interpolated y-value at the target x-coordinate.\n     * @throws IllegalArgumentException if the lengths of x and y arrays are\n     * different, if the arrays are empty, or if x-coordinates are not unique.\n     */\n    public static double interpolate(double[] x, double[] y, double target) {\n        if (x.length != y.length) {\n            throw new IllegalArgumentException(\"x and y arrays must have the same length.\");\n        }\n        if (x.length == 0) {\n            throw new IllegalArgumentException(\"Input arrays cannot be empty.\");\n        }\n\n        // Check for duplicate x-coordinates to prevent division by zero\n        Set<Double> seenX = new HashSet<>();\n        for (double val : x) {\n            if (!seenX.add(val)) {\n                throw new IllegalArgumentException(\"Input x-coordinates must be unique.\");\n            }\n        }\n\n        int n = x.length;\n        double[] p = new double[n];\n        System.arraycopy(y, 0, p, 0, n); // Initialize p with y values\n\n        for (int k = 1; k < n; k++) {\n            for (int i = 0; i < n - k; i++) {\n                p[i] = ((target - x[i + k]) * p[i] + (x[i] - target) * p[i + 1]) / (x[i] - x[i + k]);\n            }\n        }\n\n        return p[0];\n    }\n}\n","sourceCodeStart":27,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Neville.java#L27-L62","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the x array for duplicates before calling: deduplicate or remap so all x values are distinct.","If duplicate x values are legitimate (repeated measurements), aggregate y values (e.g., average) at each distinct x before interpolating.","If the duplicates come from floating-point rounding, perturb one value or use a tolerance-aware uniqueness check before passing to interpolate."],"exampleFix":"// before\ndouble[] x = {1.0, 2.0, 2.0, 4.0};\ndouble[] y = {0.0, 0.69, 0.70, 1.39};\ndouble r = Neville.interpolate(x, y, 3.0);\n\n// after — collapse duplicate x by averaging y\ndouble[] x = {1.0, 2.0, 4.0};\ndouble[] y = {0.0, 0.695, 1.39};\ndouble r = Neville.interpolate(x, y, 3.0);","handlingStrategy":"validation","validationCode":"public static boolean allXUnique(double[] x) {\n    Set<Double> seen = new HashSet<>();\n    for (double v : x) {\n        if (!seen.add(v)) return false;\n    }\n    return true;\n}\n\n// before calling:\nif (!allXUnique(x)) {\n    throw new IllegalArgumentException(\"x has duplicate coordinates\");\n}\ndouble r = Neville.interpolate(x, y, target);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Deduplicate or aggregate y-values at repeated x-coordinates before interpolating.","When x values come from floats, check uniqueness with a tolerance if rounding is expected.","Validate the x array at the data-loading boundary, not at the interpolation call site."],"tags":["math","interpolation","invalid-argument","duplicate-values"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}