Check for series of numbers in array

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 10
Reputation: lonelyrider is an unknown quantity at this point 
Solved Threads: 0
lonelyrider lonelyrider is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #11
Jun 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #12
Jun 1st, 2009
Originally Posted by ddanbe View Post
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.
  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //this only works with numbers in succession
  8. int[] range = new int[7] { 0, 2, 1, 1, 1, 4, 5 };
  9. //int[] range = new int[7] { 0, 2, 2, 1, 1, 1, 1 };
  10.  
  11. List<int> tmp = new List<int>();
  12. tmp.AddRange( range );
  13.  
  14. foreach (int i in tmp)
  15. {
  16. //can be optimised: if 3 found, jump 3 further in list
  17. Console.WriteLine(i);
  18. int first = tmp.IndexOf(i);
  19. int last = tmp.LastIndexOf(i);
  20. int t = last - first + 1;
  21. if ( t >= 3 )
  22. Console.WriteLine("--> {0} appears {1} times in the list.", i, t);
  23. }
  24. Console.ReadKey();
  25. }
  26. }
  27. }

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 = 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Check for series of numbers in array

 
0
  #13
Jun 2nd, 2009
Here's a quick, simple solution:

  1. //assume iArray is an array of numbers, and ARRAY_SIZE is the size of it
  2. int iFind = 2; //the number to look for repetitions of
  3. int iCurrent = 0;
  4. int iCount = 0;
  5. int i =0;
  6. const int Min_Reps = 2;
  7. for (i = 0; i < ARRAY_SIZE; i++)
  8. {
  9. if (iArray[i] == iCurrent)
  10. iCount++;
  11. else if (iCount > Min_Reps && iArray[i] != iCurrent)
  12. break;
  13. else
  14. {
  15. iCurrent = iArray[i];
  16. iCount = 0;
  17. }
  18. }
  19. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC