| | |
Check for series of numbers in array
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
The approach is correct and i guess you are worrying about the performance facotr or overhead created if the array.Legth is too big. But i think thats the optimised code since you have used the dictionary data structure which is pretty fast hash. But if your aim is to reduce consecutive redundancy then insert in a binary tree and read them back into another array.
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
C# Syntax (Toggle Plain Text)
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!
int[] range = new int[7] { 1, 1 1, 0, 1, 1, 1 };
or maybe can produce mistakes like:
int[] range = new int[7]{ 1, 2 3, 1, 5, 1, 4 };
maybe Im wrong, becouse I dont try it... for my purpose is the function I writed suitable ... and if anyone will solving same problem maybe this thread helps to start... Thanks for your answers and GL
Last edited by Pachrant; Jun 1st, 2009 at 10:38 am.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Here's a quick, simple solution:
Im not sure if you need to keep a list of all the times this number repeats, or just the first. This method would only find the first, min_reps represents the number of times the number must occur before it is valid.
C# Syntax (Toggle Plain Text)
//assume iArray is an array of numbers, and ARRAY_SIZE is the size of it int iFind = 2; //the number to look for repetitions of int iCurrent = 0; int iCount = 0; int i =0; const int Min_Reps = 2; for (i = 0; i < ARRAY_SIZE; i++) { if (iArray[i] == iCurrent) iCount++; else if (iCount > Min_Reps && iArray[i] != iCurrent) break; else { iCurrent = iArray[i]; iCount = 0; } } Text = "Found " + iFind + " repeating " + iCount + "times between indexes " + (i - 1 - iCount).ToString() + " and " (i - 1).ToString();
Im not sure if you need to keep a list of all the times this number repeats, or just the first. This method would only find the first, min_reps represents the number of times the number must occur before it is valid.
![]() |
Similar Threads
- How to put random numbers into an array? (Java)
- sort even and odd numbers from an array (C++)
- How can I turn a series of numbers into ASCII values and vice-versa? (C#)
- random numbers into an array (Java)
- Expandable Array Problems (C++)
- Putting Single numbers in each array (C++)
- sort even and odd numbers for an array (C)
- how do u find prime numbers in an array (C)
Other Threads in the C# Forum
- Previous Thread: List the Database in DB2 in C#
- Next Thread: Tell me about mcp and how many papers i have to given
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml





