check occurence of int in 2darray c#


I’ve got a simple game made up of a 3x4 2darray (with variable int) like so:

After each turn I want to check if the win conditions are met by comparing if 3 of the 4 int’s in only the top row are equal (these can range from 0 to 200, so it’s impossible to check for each #). So if the top row would be 1,2,2,3 the game would not be won, but it would like this: 1,4,1,1 as this row now has 3 1’s.

I’ve been trying to figure out how to write this out, but haven’t found any good ideas within my beginning c# scope. Could someone point me in the right direction?

Thanks in advance!

This assumes column/row order, but there are only four winning combinations in the row which can easily be checked with an if statement:

if (   a[0,0] == a[0,1] && a[0,1] == a[0,2])
    || a[0,1] == a[0,2] && a[0,2] == a[0,3])
    || a[0,0] == a[0,2] && a[0,2] == a[0,3])
    || a[0,0] == a[0,1] && a[0,1] == a[0,3])) {
    Debug.Log("You've Won!");

Note if four of a kind is not a winning combination, you will need to filter that out first.

If you need to check all the rows, put it in a for loop:

if (   a[i,0] == a[i,1] && a[i,1] == a[i,2])
    || a[i,1] == a[i,2] && a[i,2] == a[i,3])
    || a[i,0] == a[i,2] && a[i,2] == a[i,3])
    || a[i,0] == a[i,1] && a[i,1] == a[i,3])) {
    Debug.Log("You've Won!");

If you need to check only one row of 4 for 3 instances of the same number you can do this:
Check is the first number in the row (index 0) repeats 3 times at least, and if not, do the same for the second number (index 1). If neither of them repeats at least 3 times you don’t have numbers repeating 3 times.

This problem is easily solveable if you simplify it into groups of the same number. For instance, in your example {1,2,2,3} the groups are: 1 with 1 times it appears, 2 with 2 appearances, and 3 with 1 appearance. For {1,4,1,1}, the groups are 1 with 3 count, and 4 with 1 count. This is true no matter what the order of the array, right? If it was {1,1,1,4} it would still be 1 with 3 count, and 4 with 1 count.

Now, assuming that 4-of-a-kind is also considered a win, the the array is {X, X, X, X} with X being any number. In this case, there is only one group, X, with a count of 4. In the minimum win case, {X, X, X, Y} (and all other permutations of it), there is will always be an X with a count of 3, and a Y with a count of 1. Both win conditions can be generalized like this: One group will always have a count of 3 or more and there will always be 2 groups or less.

You can pull off this concept of groups with a System.Collections.Generic.Dictionary. With the dictionary, the element’s key would be a number in your array, and the value associated would represent how many times that number appears in your array.

The code would go something like this: You would loop through each element in the top array, and if it doesn’t already exist in the dictionary, add it to the dictionary with count of 1. If the number already exists in the dictionary, just increase the value by 1.
After looping through the array, it would then be a simple loop through the dictionary to see if there are any elements who’s values are greater or equal to 3.

For the array {1,4,1,1}, the dictionary’s elements would be {(1,3), (4,1)}. If you do not know how to use Dictionary, there are plenty of resources out there, including the this one and this one.