How to Find the Minimum Element in an Array in Java
Finding the smallest value in an array is the mirror image of finding the maximum — and it's every bit as common in interviews and real code.
This guide walks through the optimal linear-scan approach, alternative modern methods using streams, and the subtle mistakes that trip up even experienced developers.
Problem Statement
Given an array such as {45, 12, 78, 3, 25}, the task is to identify the smallest value present — here, 3.
As with maximum-finding, the optimal solution is a single O(n) pass that tracks the smallest value encountered so far.
Method 1: Linear Scan (Optimal Approach)
int[] numbers = {45, 12, 78, 3, 25};
int min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
System.out.println("Minimum element: " + min);
The loop starts at index 1 because the first element is already assigned as the initial minimum, making a self-comparison at index 0 unnecessary.
Method 2: Enhanced For Loop
int min = numbers[0];
for (int num : numbers) {
if (num < min) {
min = num;
}
}
This produces the same result with cleaner syntax and is commonly preferred when the array index isn't required.
Method 3: Java Streams
import java.util.Arrays;
int min = Arrays.stream(numbers)
.min()
.getAsInt();
As with max(), prefer .orElse(defaultValue) instead of .getAsInt() if the array might be empty.
int min = Arrays.stream(numbers)
.min()
.orElse(Integer.MAX_VALUE);
Method 4: Collections.min() for Lists
If you're working with a List<Integer> rather than a primitive array:
import java.util.Collections;
import java.util.List;
List<Integer> list = List.of(45, 12, 78, 3, 25);
int min = Collections.min(list);
Step-by-Step Explanation
Initialize min
Set min to the first element.
int min = numbers[0];
This provides a valid starting comparison point.
Loop and compare
For each subsequent element:
if (numbers[i] < min) {
min = numbers[i];
}
checks whether the current element is smaller than the smallest value found so far.
If true, update min.
Final result
After the complete traversal, min contains the smallest value in the array.
Internal Working (Memory View)
For the array:
{45, 12, 78, 3, 25}
Execution proceeds like this:
Start:
min = 45
i = 1
12 < 45
Yes
min = 12
i = 2
78 < 12
No
i = 3
3 < 12
Yes
min = 3
i = 4
25 < 3
No
Final:
min = 3
Only one comparison is performed for each element.
The min variable stays on the stack and is updated whenever a smaller value is encountered.
Real-Life Analogy
Think of finding the shortest student in a classroom lineup.
You begin by assuming the first student is the shortest.
As each new student steps forward, you compare their height with the current shortest person.
Whenever someone shorter appears, you update your record.
By the end of the lineup, you've identified the shortest student without ever sorting the entire class.
Best Practices
-
Use the linear scan (O(n)) approach—it is optimal and avoids unnecessary sorting.
-
Always guard against empty arrays before accessing
numbers[0]. -
Use
Arrays.stream(arr).min().orElse(Integer.MAX_VALUE)for concise and safe stream-based code. -
If you need both the minimum and maximum values, find them together in a single loop instead of traversing the array twice.
Common Mistakes
Initializing min to 0
int min = 0;
This breaks for arrays containing only positive numbers greater than zero because 0 would incorrectly remain the minimum.
Always initialize min using the first array element.
Sorting the array first
Sorting just to access the first element increases the time complexity from O(n) to O(n log n) unnecessarily.
Ignoring empty arrays
Trying to access:
numbers[0]
on an empty array throws ArrayIndexOutOfBoundsException.
Confusing < and >
Copying maximum-element logic and forgetting to change the comparison operator is a very common interview mistake.
Expert Tips
-
Finding both the minimum and maximum values in the same loop is a common interview optimization.
int min = numbers[0];
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
if (numbers[i] > max) {
max = numbers[i];
}
}
This still runs in O(n) time while avoiding a second traversal.
-
For custom objects, use:
Collections.min(
employees,
Comparator.comparing(Employee::getSalary)
);
-
Be careful when initializing with sentinel values like
Integer.MAX_VALUEorInteger.MIN_VALUE. Initializing with the first array element is generally safer and easier to understand.
Comparison Table
| Method | Time Complexity | Space Complexity | Recommended? |
|---|---|---|---|
| Linear scan | O(n) | O(1) | ✅ Yes – Optimal |
| Enhanced for loop | O(n) | O(1) | ✅ Yes |
Streams .min() |
O(n) | O(1) | ✅ Yes |
| Sort then take first element | O(n log n) | Varies | ❌ Inefficient |
Frequently Asked Questions
What's the time complexity of finding the minimum element in an array?
O(n).
A single pass through the array is sufficient and optimal.
Why not simply sort the array and take the first element?
Sorting requires O(n log n) time, whereas a linear scan only requires O(n).
Sorting is unnecessary when you only need the minimum value.
What if the array contains only negative numbers?
The linear scan works correctly regardless of whether the values are positive or negative, provided min is initialized with the first array element.
Can I find the minimum and maximum values in a single loop?
Yes.
Maintain two variables (min and max) and update both during the same traversal.
How do I find the minimum element in a List?
Use:
Collections.min(list);
for a List<Integer>.
What happens if the array is empty?
Accessing numbers[0] throws an ArrayIndexOutOfBoundsException.
Using Streams, .min() returns an empty OptionalInt, so handle it with .orElse() when appropriate.
Is there a built-in Java method to find the minimum directly?
Yes.
For primitive arrays:
Arrays.stream(arr).min();
For lists:
Collections.min(list);
How do I find the minimum element in a 2D array?
Traverse every row and every column while maintaining a running minimum.
Alternatively, flatten the 2D array using Streams and then call .min().
Conclusion
Finding the minimum element in an array is one of the most fundamental array operations in Java.
The linear scan remains the best solution because it provides:
-
O(n) time complexity
-
O(1) extra space
-
Excellent readability
-
Optimal interview performance
Whether you use a classic loop, an enhanced for loop, or Java Streams, every optimal solution examines each element exactly once, making the algorithm both simple and highly efficient.