Find the largest array value
Find the largest number in a non-empty integer array without sorting it.
Course progress
Problem 8 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
numbers = [12, 7, 31, 18, 4]
Expected output
31
What is happening?
A left-to-right scan updates the maximum from 12 to 31. None of the remaining values exceeds 31.
Solution strategy
Build the right mental model
Treat the first item as the current maximum, then replace it whenever a larger item appears.
Core concepts
arrays · linear scan
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for largest-array-value
using System;
class Program
{
static int FindLargest(int[] numbers)
{
if (numbers.Length == 0) throw new ArgumentException("Array cannot be empty.");
int largest = numbers[0];
foreach (int number in numbers)
if (number > largest) largest = number;
return largest;
}
static void Main()
{
Console.WriteLine(FindLargest(new[] { 12, 7, 31, 18, 4 }));
}
}Knowledge check
Questions students often ask
What is the main idea behind find the largest array value?
Treat the first item as the current maximum, then replace it whenever a larger item appears.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.