Start learning free

LESSON 142 OF 347 · LOOPS & GLOBAL VARIABLES

Max in Array in C#: Track the Largest Value with foreach

Practice Read the full explanation

Given an array of integers, return the maximum value from the array.

Example:

Input: nums = [2, 7, 11, 8, 11, 8, 3, 11]
Output: 11
Explanation: The maximum value is 11

Note:

Do not use builtin functions such as Math.Max(), it would defy the purpose of the challenge. Write the whole code yourself.

STEP 1 OF 4
    Ctrl + Enter to check

    C# · LOOPS & GLOBAL VARIABLES

    Max In Array in C#, explained

    Problem overview

    MaxInArray(int[] nums) returns the largest value in nums. For [2, 7, 11, 8, 11, 8, 3, 11] the answer is 11. A value that appears more than once, like 11 here, is still just one answer.

    The lesson wants you to write the search yourself with a loop, rather than reach for a built-in that scans the whole array for you.

    Approach: keep the best value seen so far

    You can't know the maximum until you have seen every element, but you can keep a running champion. Start by treating the first element as the largest. Then look at each element in turn: if it beats the champion, it becomes the new champion. When the loop ends, nobody has beaten the champion, so it is the maximum.

    For the example, the champion changes like this as the loop passes each value:

    value:    2   7  11   8  11   8   3  11
    champion: 2   7  11  11  11  11  11  11
    

    Step by step

    1. Start maxVal at nums[0]. It is tempting to start at 0, but that is wrong when every number is negative: for [-5, -2, -9] the starting 0 would beat all of them and be returned, even though it isn't in the array. The first element is always a legitimate candidate.
    2. Loop with foreach. foreach (int val in nums) gives each value in order. The first pass compares nums[0] with itself, which is harmless.
    3. Update the champion. maxVal = Math.Max(maxVal, val) keeps the larger of the two. Math.Max(a, b) returns whichever argument is bigger, and it is the same as writing if (val > maxVal) { maxVal = val; }.
    4. Return maxVal after the loop, when every element has had its chance.

    The solution in C#

    class Solution {
        public int MaxInArray(int[] nums) {
            int maxVal = nums[0];
            foreach (int val in nums) {
                maxVal = Math.Max(maxVal, val);
            }
            return maxVal;
        }
    }
    

    Points specific to C# are listed here:

    • Math.Max lives in the System namespace. It is a static method, so you call it on the class: Math.Max(a, b). It has overloads for int, long, double and the other numeric types, and the int version is chosen here because both arguments are int.
    • The comparison form is equally valid. If you prefer to see the decision, if (val > maxVal) { maxVal = val; } inside the loop gives the same result, and it extends more easily when you need to remember more than the value.
    • Indexing starts at 0. nums[0] is the first element and nums[nums.Length - 1] the last. The array size comes from the Length property.
    • An empty array throws. nums[0] on an array with no elements raises an IndexOutOfRangeException at run time. An array has no "first element" to start from, and there is also no sensible maximum of nothing.

    Complexity

    Time: O(n). Each of the n elements is compared once. The maximum of an unsorted array can't be known without looking at every element, so a linear scan is the best you can do.

    Space: O(1). The method keeps maxVal and the loop variable.

    Edge cases and common mistakes

    • All negative numbers. [-5, -2, -9] must return -2. Starting from nums[0] handles it, while starting from 0 doesn't.
    • A single element. The champion is that element and nothing can beat it, so the method returns it.
    • Duplicates of the maximum. They don't matter, because the champion is replaced only by a larger value, and an equal one changes nothing.
    • Returning inside the loop. The method would stop after the first element and return it.
    • Starting from a made-up number. Starting from int.MinValue also works, since nothing is smaller, but nums[0] is simpler and works for every numeric type.
    • Returning the index. The problem asks for the value. Tracking an index is a different lesson.

    Testing

    • [2, 7, 11, 8, 11, 8, 3, 11] gives 11.
    • [-5, -2, -9] gives -2.
    • [4] gives 4.
    • [3, 3, 3] gives 3.
    • [1, 2, 3, 4, 5] gives 5, with the maximum at the end.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free