﻿ C# - Find the number of 5s next to each other in an array

# C#: Count the number of two 5's are next to each other in an array of integers

## C# Sharp Basic Algorithm: Exercise-38 with Solution

Write a C# Sharp program to count the number of two 5's next to each other in an array of integers. Count the situation where the second 5 is actually a 6.

Visual Presentation:

Sample Solution:-

C# Sharp Code:

``````using System;
using System.Linq;

namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Calling the 'test' method with different integer arrays
Console.WriteLine(test(new[] { 5, 5, 2 }));         // Output: 1
Console.WriteLine(test(new[] { 5, 5, 2, 5, 5 }));  // Output: 2
Console.WriteLine(test(new[] { 5, 6, 2, 9 }));     // Output: 0
Console.ReadLine(); // Keeping the console window open
}

// Method to count occurrences of specific number patterns in an integer array
public static int test(int[] numbers)
{
var ctr = 0; // Counter to track occurrences
for (var i = 0; i < numbers.Length - 1; i++) // Loop through the array till the second-to-last element
{
// Check for the pattern '5, 5' or '5, 6'
if (numbers[i].Equals(5) && (numbers[i + 1].Equals(5) || numbers[i + 1].Equals(6)))
{
ctr++; // Increment the counter if the pattern is found
}
}
return ctr; // Return the count of occurrences
}
}
}
```
```

Sample Output:

```1
2
1```

Flowchart:

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿