| | |
Check for series of numbers in array
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
Hello everybody...
Can anyone help me with this?:
I want to check out an array, if there is any number which is 3, or more times in a row.
IE:
int[] tmp= new int[7]{0,2,1,1,1,4,5}; // yes 3 duplicities starts at index 2
int[] tmp= new int[7]{0,2,2,1,1,1,1}; // yes 4 duplicities starts at index 3
any ideas?
Thanks for any answer.
Can anyone help me with this?:
I want to check out an array, if there is any number which is 3, or more times in a row.
IE:
int[] tmp= new int[7]{0,2,1,1,1,4,5}; // yes 3 duplicities starts at index 2
int[] tmp= new int[7]{0,2,2,1,1,1,1}; // yes 4 duplicities starts at index 3
any ideas?
Thanks for any answer.
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
What have you tried so far?
What code do you already have and what are the problems you have with it?
But I gave up before end becouse it not seemd to me as good method.
I hope there is more sophisticated method to do that. Maybe allready written function... Dont Know.
I was thinking about cast the array to string and use regex ... but Im not sure Im able to define a patern and use it.
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
To make it more clear. (or otherwise >) ) :
I have 2 dimensional array (12 rows and 12 cols) filled with numbers {1,2,3,4,5} and what I have to do is remove from it (make it 0) occurrences of 3 and more (max 12) same numbers in a row. I have to do that in horizontal and vertical way.
hope this helps undestand. Any sugestions?
I have 2 dimensional array (12 rows and 12 cols) filled with numbers {1,2,3,4,5} and what I have to do is remove from it (make it 0) occurrences of 3 and more (max 12) same numbers in a row. I have to do that in horizontal and vertical way.
hope this helps undestand. Any sugestions?
•
•
•
•
Originally Posted by Pachrant
So, I tried to iterate thru the array using for loop and at each iteration remember previous number and count how many numbers are same.
If it works you can always improve afterward if at all possible.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
I will, dont worry, but I hoped that it will be a better (easier) solution, how to do that.
IE cast it to the some collection and use any method or as I mention before cast it to the strig and use regex or any function like contains()...
I´m realy disappointed about it.... Still hope that anyone direct me at the right way. Thx for any advice...
IE cast it to the some collection and use any method or as I mention before cast it to the strig and use regex or any function like contains()...
I´m realy disappointed about it.... Still hope that anyone direct me at the right way. Thx for any advice...
Last edited by Pachrant; May 30th, 2009 at 10:45 pm.
•
•
Join Date: May 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
A List or an Array has a FindAll method. This is what I should use as a start. Start coding!
Any problems with it, come back here.
Its not best solution, but it works.
Thank for your time.
C# Syntax (Toggle Plain Text)
public Dictionary<int,int> FindSeries(string[] v_Patern , string[] v_Array) { Dictionary<int,int> result=new Dictionary<int,int>(); string s= String.Join("", v_Array,0,v_Array.Length); string p= String.Join("", v_Patern,0,v_Patern.Length); string patern=""; for (int l = 0; l < v_Array.Length; l++) patern += p; for (int i = 0; i < v_Array.Length; i += v_Patern.Length) { patern = patern.Substring(0, patern.Length - p.Length); if (patern.Length >= 3) // more than 3 occurrances of patern required { while (s.IndexOf(patern) != -1) { bool b = true; foreach (KeyValuePair<int, int> d in result) if ((s.IndexOf(patern) > d.Key && s.IndexOf(patern) < d.Key + d.Value)) b = false; if (b) { result.Add(s.IndexOf(patern), patern.Length); break; } } } } return result; //return value key: index, value number of occurrances }
Last edited by Pachrant; May 31st, 2009 at 4:19 pm.
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.
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!
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
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 2007 access algorithm array barchart bitmap box broadcast c# camera check checkbox client combobox control conversion cs4 csharp custom customactions database datagrid datagridview dataset date datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ handler httpwebrequest image index input install java keypress label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resolved. richtextbox search security server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml






