# [RESOLVED] search 2d array, return index(s)?

• Mar 7th, 2018, 11:48 AM
CWITT
[RESOLVED] search 2d array, return index(s)?
I have a 2D array (see here for the how/why) and I'm trying to figure out how (or even if it's possible) to search for a given value (in this case a custom enum value) and return the location (index) of that value in the array.

Or am I just spinning my wheels?

Edit:
I don't know if it is the best route, but I created my own search function to do this.
• Mar 7th, 2018, 12:08 PM
Shaggy Hiker
Re: search 2d array, return index(s)?
Sure, it's possible to search for a given value, and you say you've created your own search function, which seems like a good thing to do. There may be multiple ways you can go about it, but the typical way is to just iterate through both dimensions of the array, and that would be as efficient as anything else unless there are special properties of the array that make some other search method more effective.
• Mar 7th, 2018, 12:11 PM
CWITT
Re: search 2d array, return index(s)?
It's a fairly simplistic array of values.

Code:

```Function ArraySearch_Value(ByVal ArrayList As TileData.MoveType(,), ByVal Value As TileData.MoveType) As Point         For y = 0 To 4             For x = 0 To 4                 If ArrayList(x, y) = Value Then                     Return New Point(x, y)                 End If             Next         Next     End Function```
Code:

```  Function ArraySearch_Values(ByVal ArrayList As TileData.MoveType(,), ByVal Value As TileData.MoveType) As List(Of Point)         Dim TPL As New List(Of Point)         For y = 0 To 4             For x = 0 To 4                 If ArrayList(x, y) = Value Then                     TPL.Add(New Point(x, y))                 End If             Next         Next         If TPL.Count = 0 Then             Return Nothing         Else             Return TPL         End If     End Function```
• Mar 7th, 2018, 12:19 PM
Shaggy Hiker
Re: [RESOLVED] search 2d array, return index(s)?
Yeah, it's pretty unlikely that you can do anything more efficient than that. If the arrays are sorted, you might be able to do something like a binary search to improve the performance, but that wouldn't really matter unless your array dimensions are in the tens of thousands.

Alternatively, if the values were all unique, you could maintain a Dictionary(of String, Point) in addition to the array. Looking something up from the dictionary would be far faster, but it wouldn't work if the values weren't unique, and you'd have to decide whether the overhead of maintaining both the array and the dictionary is worth it (if you changed one you'd have to change the other).

A third option is that a 2D array can always be a 1D array, cause that's what they are in memory. In memory, the first dimension can be thought of as the row, and the 2D array is a series of contiguous chunks, each of which is a row, so any one item is (rowIndex * rowSize) + columnIndex. This really doesn't speed up searching all that much, except that a 1D array would be easier to search with things like LINQ...which wouldn't give you any advantage at all, but could look pretty compact.
• Mar 7th, 2018, 12:23 PM
CWITT
Re: [RESOLVED] search 2d array, return index(s)?
Yeah, for my use the 2D array isn't for gathering or sorting data but rather holding the "location" of a move and it's own location relative to those moves (for my tiles, not that that explanation should mean anything without a lot more context).

Thanks for the input :)