This is my own humble contribution to your problem. If it is better or worser I don't know, but it might give you some ideas.
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //this only works with numbers in succession int[] range = new int[7] { 0, 2, 1, 1, 1, 4, 5 }; //int[] range = new int[7] { 0, 2, 2, 1, 1, 1, 1 }; List<int> tmp = new List<int>(); tmp.AddRange( range ); foreach (int i in tmp) { //can be optimised: if 3 found, jump 3 further in list Console.WriteLine(i); int first = tmp.IndexOf(i); int last = tmp.LastIndexOf(i); int t = last - first + 1; if ( t >= 3 ) Console.WriteLine("--> {0} appears {1} times in the list.", i, t); } Console.ReadKey(); } } }
Another possibility might be you construct a binary tree.
Loop through your input.
If the number is not in the tree insert it.
If the number is already in the tree augment a count variable you keep with the number.
Afterwards, traverse the tree and note which numbers have a count >=3.I'm not so good at that but look here for info : http://msdn.microsoft.com/en-us/library/ms379572.aspx
Succes!
Ok, I dont compile your code, but I think that it cant handle more same paterns in array like:
int[] range = new int[7] { 1, 1 1, 0, 1, 1, 1 };
or maybe can produce mistakes like:
int[] range = …