When dealing with two-dimensional coordinates like (0, 1), (0, 2)
, and (2, 3)
, I initially considered using arrays such as [[0, 1], [0, 2], [2, 3]]
. However, I quickly realized that this approach presented challenges when it came to efficiently looking up a specific coordinate.
I could have implemented a search function for an array of arrays, but this would require iterating over each item to find a match in the worst-case scenario.
For example, if I wanted to find the coordinate 0, 2
, using arr.indexOf(value)
wouldn't work since the value is an array. So, I then considered storing coordinates as strings like arr.push('01')
.
The downside of storing coordinates as strings is that computations involving them would require converting between string and integer values multiple times.
Is there a more effective approach that allows for efficient lookup without resorting to sacrificing the integrity of the data by converting it to a string?