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.

Recommended Answers

All 12 Replies

What have you tried so far?
What code do you already have and what are the problems you have with it?

Even if you have only a few lines of code, just post them here.
Don't be affraid if it is really bad code, we are here to help you. But please show some effort.

What have you tried so far?
What code do you already have and what are the problems you have with it?

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.

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.

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?

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.

Give it a try!!!
If it works you can always improve afterward if at all possible.

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...

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.

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.

Im using this:
Its not best solution, but it works.
Thank for your time.

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
        }

but if anyone find, or know a better solution Im alble to rewrite it

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!

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.

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 = 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 ;)

Here's a quick, simple solution:

//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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.