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 for and 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

Using sample array: 12, 5, 7, 3, 2, 8, 10.

Live result
Press "Find minimum".

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 min
1

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 arrayOutput
[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

TaskTimeExtra space
Find minimum in array of size nO(n)O(1)

Summary

  • Track one running minimum while scanning once.
  • The approach naturally handles negatives and duplicates.
  • Complexity is O(n) time and O(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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful