I have a list that contains ints, all from 0-2. Now I have to check if any of these numbers occurs exactly 3 times. How would one check that?
Example:
{ 2, 1, 0, 0, 1, 0 } //this is true
{ 1, 1, 2, 0, 0 } //this is false
{ 0, 0, 0, 1, 1, 1, 2, 2, 2 } //this is true
You could use LINQ for this:
bool containsNum3x =
list
.GroupBy(i => i) // group the 0s into a subset, the 1s into a subset, etc
.Any(s => s.Count() == 3); // check if the size of any subset is exactly 3
Docs:
You might need to add using System.Linq
in order at the top of your code file.