Sobes.tech
Intern — Senior

Improving the efficiency of finding the largest number in a list

livecode

Task condition

The function FindMax implements the search for the maximum value in a list of integers. Is its implementation correct? How can it be improved in terms of performance?

private int FindMax(List<int> numbers)
{
    int currentMax = int.MinValue;
    if (numbers != null)
    {
        foreach (int value in numbers)
        {
            if (currentMax < value)
            {
                currentMax = value;
            }
        }
    }
    return currentMax;
}