Pattern matching in an array or collection If I had an array of integers like

+--------------------------------------------+
| 1| 0| 1| 2| 0| 1| 0| 1| 0|-1| 0| 2| 0|-2| 0|  &gt; VALUES
+--------------------------------------------+
| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|  &gt; INDEX
+--------------------------------------------+


I am looking for 0 which occurs 7 times in the array above.

But I need to find indexes of 0s in the array like so:


I can find 0's that occur only after a slot with a Abs(value) > 1
(rising of falling)
I can find 0's that occur only after a slot with a value > 1
(rising)
I can find 0's that occur only after slot with a negative Abs(value)


  1 (falling)



So, looking at array above my logic should show indexes


1, 4, 12, 14 - 1st and REST (rising or falling)
1, 4, 12     - 1st and RISING
1, 14        - 1st and FALLING


The indexes above are valid since the previous value was (Abs(previous) > 1). On the other hand, 0's at index 6, 8, and 10 are of no interest since the previous array value was either 1 or -1 (so Abs(previous) &lt;= 1)

My code below works but I feel there could be a simpler more elegant solution to this

public static void Main(string[] args)
{
    int[] array = new int[15] { 1, 0, 1, 2, 0, 1, 0, 1, 0, -1, 0, 2, 0, -2, 0 };

    bool found1 = true;
    for (int i = 0; i &lt; array.Length; i++)
    {
        if (array[i] == 0)
        {
            if (found1)
            {
                Console.WriteLine(i); //print 1st index of 0
                found1 = false;
            }
            else
            {
                if (Math.Abs(array[i - 1] - array[i]) &gt; 1)
                {
                    Console.WriteLine("All: " + i);  // print every other that fits
                    //1, 4, 12, 14
                }
            }
        }
    }

    found1 = true;
    for (int i = 0; i &lt; array.Length; i++)
    {
        if (array[i] == 0)
        {
            if (found1)
            {
                Console.WriteLine(i); //print 1st index of 0
                found1 = false;
            }
            else
            {
                if (Math.Abs(array[i - 1] - array[i]) &gt; 1
                    &amp;&amp; (array[i - 1] - array[i]) &gt; 1)
                {
                    Console.WriteLine("Up: " + i);  // print rising
                                                    //1, 4, 12
                }
            }
        }
    }

    found1 = true;
    for (int i = 0; i &lt; array.Length; i++)
    {
        if (array[i] == 0)
        {
            if (found1)
            {
                Console.WriteLine(i); //print 1st index of 0
                found1 = false;
            }
            else
            {
                if (Math.Abs(array[i - 1] - array[i]) &gt; 1
                    &amp;&amp; (array[i - 1] - array[i]) &lt; 1)
                {
                    Console.WriteLine("Down: " + i);  // print falling
                                                      //1, 14
                }
            }
        }
    }

    Console.ReadLine();
}
You are unnecessarily looping through the array 3 times (only one loop is required). Its not clear how you will be using the results, but assuming you need to store them, then the code can be simplified to

int[] array = new int[15] { 1, 0, 1, 2, 0, 1, 0, 1, 0, -1, 0, 2, 0, -2, 0 };
// Define 3 lists from the results
List&lt;int&gt; all = new List&lt;int&gt;();
List&lt;int&gt; up = new List&lt;int&gt;();
List&lt;int&gt; down = new List&lt;int&gt;();
// Get the index of the first zero value and add to the lists
int first = Array.IndexOf(array, 0);
all.Add(first);
up.Add(first);
down.Add(first);
// Loop the remaining items in the array and add to the appropriate lists
for (int i = first + 1; i &lt; array.Length; i++)
{
    int current = array[i];
    int previous = array[i - 1];
    if (current != 0)
    {
        continue;
    }
    if (Math.Abs(previous - current) &gt; 1)
    {
        all.Add(i);
        if (previous - current &gt; 1)
        {
            up.Add(i);
        }
        else if (previous - current &lt; 1)
        {
            down.Add(i);
        }
    }
}


The result is

all contains [1, 4, 12, 14], up contains [1, 4, 12] and down contains [1, 14]