This site uses strictly necessary cookies. More Information

X- Home /

# check occurence of int in 2darray c#

Hello,

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!

**Answer** by hoy_smallfry
·
May 07, 2013 at 06:37 PM

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.

At the worst case, you'll have 4 elements in the dictionary you have to loop through, and at the best case it'll only be 1, but you can cut it having to loop through 3 and 4 elements altogether, because no matter what a dictionary count of 3 or 4 is an automatically a lose condition, because there's no way any of those groups could have a count of 3 or more. You can probably also have it consider a dictionary count of 1 to be an automatic win, since that would be guaranteed to have a count of 3 or more.

thanks for your idea, I've been trying to work this out and have writtin this piece of code as the solution:

```
Dictionary<int,int> dic = new Dictionary<int, int>();
//---check p1
for (int d=0;d<5;d++)
{
if (holders[d,0] != 0)
{
if (!dic.Contains$$anonymous$$ey(holders[d,0]))
{
dic[holders[d,0]] = 0;
}
dic[holders[d,0]]++;
}
}
foreach (int k in dic.$$anonymous$$eys)
{
if (k>=3)
{
Awins = true;
}
}
```

thanks so much for your help!

edit: i asked a question not related to this, but found a solution, so deleted that q to keep this thread clean.

**Answer** by robertbu
·
May 07, 2013 at 07:56 PM

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!");
break;
}
```

thanks, this actually solved my problem, but created a new one as it would also accept 4 0's as a win (something which I didn't mention in my question, so the fault is on my side) but the reason why I chose another answer as the solution! thanks a lot!

**Answer** by MeLight
·
May 07, 2013 at 06:24 PM

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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.