views:

130

answers:

4

I have seven words in the array:

string[7] = {x,x,x,x,x,x,x};

the x is generated from another array:

string[4]={a,b,c,d};

that means each x can be either a or b or c or d. It is randomly generated. this could be an example:

string[7]= {a,a,d,a,a,c,a}

my question is how can I check if there are five x which has the same value?

This is for a poker app Im working on.

+10  A: 

You can use Linq to find the largest number of equal items and test if this is 5 or more:

int maxCount = s.GroupBy(x => x).Select(x => x.Count()).Max();
Mark Byers
__** slick! **__
Shane Cusson
Don't you just love Linq ?
HaxElit
Thanks. worked like charm. :D Very useful!
Alexandergre
+1  A: 

You can group the similar items and find it any group have five or more

from word in new [] { "a", "a", "a", "b", "a", "a", "b" }
group word by word into wordGroup
where wordGroup.Count() >= 5
select wordGroup.Key
bdukes
+3  A: 

You can do it like this:

    List<string> values = new List<string> {"a", "a", "d","a", "a", "c", "a"};

    int count = values.FindAll(id => id == "a").Count();
Jason Heine
This would only find out the number of "a" values. Remember that he's looking for 5 values that match - but the values can be anything.
Michael Burr
Ahh yes, I see now. I had to re-read his question. Thanks
Jason Heine
+1  A: 

Sort the array, after that you are sure that if there are five or more of the same value, the middle value is one of them. Count how many:

Array.Sort(words);
int cnt = 0;
Array.ForEach(words, s => { if (s == words[3]) cnt++; });
Guffa