Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to find peaks in array?

C#  |  .NET  |  Algorithms

Let's imagine a task that you need to prepare the code finding peaks in an array. A peak is any element which value is higher than values of its neighbors. Of course, an array can contain many peaks and you should detect all of them. How can you solve this problem?

The solution is shown right below:

int[] GetPeaks(int[] a)
{
    List peaks = [];
    
    // Check whether the first element is a peak.
    if (a[0] >= a[1]) 
    {
        peaks.Add(a[0]);
    }
    
    // Check whether any element
    // (except the first and the last) is a peak.
    for (int i = 1; i < a.Length - 1; i++)
    {
        if (a[i - 1] <= a[i] && a[i] >= a[i + 1]) 
        {
            peaks.Add(a[i]);
        }
    }
    
    // Check whether the last element is a peak
    if (a[^1] >= a[^2])
    {
        peaks.Add(a[^1]);
    }
    
    return [.. peaks];
}

You can check this code by the following lines:

int[] array = [10, 8, 7, 5, 2, 7, 4, 8, 2, 9];
Console.WriteLine($"Peaks are: {string.Join(", ", GetPeaks(array))}");

The results are shown as follows:

Peaks are: 10, 7, 8, 9

If you want to learn much more about arrays, as well as about other data structures, I encourage you to take a look at my book, namely the Second Edition of C# Data Structures and Algorithms, published by Packt Publishing in 2024. You can buy it here.

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Do you follow your favorite creators? If so, check it out what products they recommend and buy them at: https://refspace.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.