Java | 2 Solutions
Solution 1 | 13 ms\n\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        int result = -1, min = Integer.MAX_VALUE;\n   
# Solution 1 | 13 ms\n```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        int result = -1, min = Integer.MAX_VALUE;\n        for (int i = 0; i < points.length; i++) {\n            if (x == points[i][0] || y == points[i][1]) {\n                int manh = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n                if (min > manh) {\n                    min = manh;\n                    result = i;\n                }\n            }\n        }\n        return result;\n    }\n}\n```\n\n\n# Solution 2 | 6 ms\n```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        int result = -1, minSum = Integer.MAX_VALUE;\n        for (int i = 0; i < points.length; i++) {\n            if (points[i][0] == x) {\n                int manh = Math.abs(y - points[i][1]);\n                if (minSum > manh) {\n                    minSum = manh;\n                    result = i;\n                }\n            } else if (points[i][1] == y) {\n                int manh = Math.abs(x - points[i][0]);\n                if (minSum > manh) {\n                    minSum = manh;\n                    result = i;\n                }\n            }\n        }\n        return result;\n    }\n}\n```
['Java']