Check all points - O(n) time and O(1) space JavaScript solution
This is staraightforward problem, example implementation in JavaScript:\n\n\nvar nearestValidPoint = function(x, y, points) {\n    // keep track of both the sho
This is staraightforward problem, example implementation in JavaScript:\n\n```\nvar nearestValidPoint = function(x, y, points) {\n    // keep track of both the shortest distance and its index\n    let ans = -1, ansDist = Number.MAX_SAFE_INTEGER;\n    \n    for (let i = 0; i < points.length; i++) {\n        const [px, py] = points[i];\n        if (px === x || py === y) {\n            const dist = Math.abs(x - px) + Math.abs(y - py);\n            if (dist < ansDist) {\n                ansDist = dist;\n                ans = i;\n            }\n        }\n    }\n    \n    return ans;\n};\n```\n
['JavaScript']