Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
static void Main(string[] args)
        {
          
            int counter =0 ;
            int[] arr = { 1, 7, 5, 4, 4, 4, 0, 8, 2, 2, 2, 3, 1, 1, 1 };
                    
           for (int i = 0; i < arr.Length; i++)
            {
                   
                if (arr[i] == arr[i+1])
                {
                    counter++;
                }
               else 
                { 
                    counter = 0;
                }
                if (counter == 2)
                {
                    Console.WriteLine(arr[i-1]);
                }
                
            }
           Console.ReadKey();
        }
    }
}


What I have tried:

i try to make the output ptint the starting position of every three identical trio in the array(4,2,1).
tried to change some values of the index and the counter but coudlnt figure it out.
what is the best way do to so?
thanks for the help.
Posted
Updated 21-May-22 0:03am
v3
Comments
Richard MacCutchan 20-May-22 11:19am    
The first trio is obviously at position 0. Where do you think the next trio will be positioned?
Member 15643928 20-May-22 11:24am    
i mean that i want to print the first position of every three identical numbers.
i want it to print tje first position of 4 ,first position of 2 snd first position of 1 with for loop.
thanks
Richard MacCutchan 20-May-22 11:30am    
that is slightly more complicated. So starting at the beginning you need to make a note of the position each time you find a new number. Then compare the next two, and if they are the same you can print the position and the number. Continue doing this until you have tested all numbers.
[no name] 20-May-22 12:01pm    
Is 1111, one trio or 2?
Member 15643928 20-May-22 12:13pm    
there are only 3 ones in a row at the end

So run your loop and count the number of occurrences of a value in a row. When it changes you know how many the previous value was:
Values: 1, 7, 5, 4, 4, 4, 0, ...
Prev  : ?  1  7  5  4  4  4  0
Count : 0  1  1  1  2  2  3
It the count is 3, you can print the value - 4 - and the start position from the current index - 3.
Each time it changes, set the count to 1 and save the value for the next time round the loop.

Give it a try on paper, and you'll see what I mean.
 
Share this answer
 
v3

You could use Linq to solve this problem.

C#
//this assumes that 3 consecutive numbers is the maximum allowed
int[] arr = { 1, 7, 5, 4, 4, 4, 0, 8, 2, 2, 2, 3, 1, 1, 1 };
//select a value tuple consisting of the number n and its index 'i' in the array
var query = arr.Select((n, i) => (n, i))
    .Where(t => t.i  < arr.Length - 2 && t.n == arr[t.i + 1] && t.n == arr[t.i + 2]);
foreach (var t in query)
    Console.WriteLine($"Number {t.n} trio starts  at index {t.i}");
//prints
//Number 4 trio starts  at index 3
//Number 2 trio starts  at index 8
// Number 1 trio starts  at index 12

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900