Find Minimum Value of an Array in Java
What you’ll learn
- How one-pass linear scan finds minimum value.
- How to write a reusable Java method
findMin. - Why this also works when numbers are negative.
Prerequisites
Java arrays, loops, conditions, and console output.
- You can iterate an array with
forand access values by index. - You understand that minimum means the smallest numeric value in the list.
The idea
Keep one current winner (min). Scan left to right and replace it whenever you see a smaller value. One pass is enough.
Live preview
Algorithm
Initialize
Set min to the first element.
Scan
Loop from index 1 to the end.
Update
If current value is smaller, replace min.
Return
Return final min.
📜 Pseudocode
Pseudocode
function findMin(arr):
min = arr[0]
for i from 1 to arr.length - 1:
if arr[i] < min:
min = arr[i]
return min1
Find minimum (reference program)
java
public class Main {
static int findMin(int[] arr) {
int minVal = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
public static void main(String[] args) {
int[] array = {12, 5, 7, 3, 2, 8, 10};
int minValue = findMin(array);
System.out.println("Minimum value in the array: " + minValue);
}
}📤 Output
Minimum value in the array: 2
2
All elements are negative
java
public class Main {
static int findMin(int[] arr) {
int minVal = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
public static void main(String[] args) {
int[] negatives = {-9, -3, -1, -7};
System.out.println("Minimum (most negative): " + findMin(negatives));
}
}📤 Output
Minimum (most negative): -9
Optimization tips
Use one scan and avoid sorting when only minimum is required.
Initialize with first element, not arbitrary constants.
❓ FAQ
It means the smallest value among all elements in the array.
It gives a valid initial candidate from the data itself.
Empty arrays have no minimum value. Handle arr.length == 0 before calling findMin.
Yes. The smallest value is still found correctly, even when all values are negative.
No. Sorting is more work. One scan is enough when you only need minimum.
O(n) time and O(1) extra space.
Input/output examples
| Input array | Output |
|---|---|
[12, 5, 7, 3, 2, 8, 10] | 2 |
[-9, -3, -1, -7] | -9 |
Edge cases
Empty
No first element
Define behavior for empty arrays: throw, sentinel, or OptionalInt.
Single
One element
That element is the minimum.
Duplicate
Same min repeated
Result remains the same minimum value.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Find minimum in array of size n | O(n) | O(1) |
Summary
- Track one running minimum while scanning once.
- The approach naturally handles negatives and duplicates.
- Complexity is
O(n)time andO(1)extra space.
Did you know?
Finding minimum in an unsorted array takes one left-to-right pass: O(n) time and O(1) extra space.
8 people found this page helpful
