Find the smallest array value
Find the smallest integer in a non-empty array without sorting the array.
Course progress
Problem 51 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 = [7, -4, 12, 0, 3]
Expected output
-4
What is happening?
The running minimum starts at 7 and changes to -4. No later value is smaller, so -4 is returned.
Solution strategy
Build the right mental model
Use the first value as the current minimum, then replace it whenever the scan finds a smaller value.
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 smallest-array-value
using System;
class Program
{
static int FindSmallest(int[] numbers)
{
if (numbers.Length == 0) throw new ArgumentException("Array cannot be empty.");
int smallest = numbers[0];
foreach (int number in numbers)
if (number < smallest) smallest = number;
return smallest;
}
static void Main()
{
Console.WriteLine(FindSmallest(new[] { 7, -4, 12, 0, 3 }));
}
}Knowledge check
Questions students often ask
What is the main idea behind find the smallest array value?
Use the first value as the current minimum, then replace it whenever the scan finds a smaller value.
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.