943,712 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1515
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 1st, 2009
0

Re: Check for series of numbers in array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lonelyrider is offline Offline
10 posts
since May 2009
Jun 1st, 2009
0

Re: Check for series of numbers in array

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pachrant is offline Offline
7 posts
since May 2009
Jun 2nd, 2009
0

Re: Check for series of numbers in array

Here's a quick, simple solution:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: List the Database in DB2 in C#
Next Thread in C# Forum Timeline: Tell me about mcp and how many papers i have to given





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC