[Question] Best way for array matching

So I am trying to learn how to use Unity, I was trying to recreate a game I made on gamemaker that I seem to be having a hard time doing on Unity.

I have an array of colors.

0 1 2 3
0 R R G
1 R B Y
2 B Y Y
3 Y G Y

So what is happening is I want to find all the colors touching each other. The 3 reds in the top left and the 4 yellow in the bottom right. In this game, 3 or more counts as a match and can be horz and vert.

Right now I have been doing checks for each spot. So checks for up, down, left, right for matches. (Only checks once after the user moves). But it can get slow at times. I know there is a better way, I just think I need a push in the right direction.

Would pathing work better?

Thanks for your time!

How can this really be slow, if your grid is only a few elements? Or is this just a tiny example of some huge array that you’re dealing with? Cus a* pathfinding is not exactly a fast algorithm either.

You could do a flood fill algorithm for example, starting the flood at every cell, and counting how many cells you find. Then mark the cell as ‘used’ so you don’t re-match it in the next check.

Thanks for you reply, this is just a small example.

I only asked about pathfinding because a coworker of mine suggested trying that. It didn’t make sense in my head to do a pathfinding as the matches on average are 4 to 5 colors.

I’ll try that, thank you!