Java Array Program · A02
Find the Smallest Element in an Array in Java
Walk through the array once and keep the smallest value seen so far. Starting from the first element avoids guessing a "large enough" initial value.
Problem Statement
Write a method that returns the smallest value in an int array. The array can contain negative numbers, duplicates and very large values.
An empty array has no smallest element, so the method throws an IllegalArgumentException.
Input/Output Examples
[12, -5, 48, 7]Output-5Explanation: -5 is smaller than every other element.[9, 9, 9]Output9Explanation: when every element is the same, that value is the minimum.[2147483647]Output2147483647Explanation: the only element is returned, even though it equals Integer.MAX_VALUE.Visual Explanation
[12, -5, 48, 7]: min starts at 12 and changes only when a smaller value appears (purple).After the first element, each value is compared with the current min once. Because min always holds a real element, the method never returns a value that is not in the array.
Approaches
The loop shows the comparison explicitly. The stream version asks IntStream for its minimum.
Approach 1: Single Loop (Recommended)
Idea: start with the first element and replace min whenever a smaller element appears.
- Throw an exception if the array is empty.
- Set
min = arr[0]. - For every later element, if it is less than
min, store it inmin. - Return
min.
Time: O(n) · Space: O(1).
Approach 2: IntStream.min()
Idea: call Arrays.stream(arr).min(), which returns an empty OptionalInt for an empty array.
- Create an
IntStreamfrom the array. - Call
min(). - Unwrap the result with
orElseThrow().
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Single Loop (Recommended)
Using < rather than <= keeps the first occurrence, which matters if you later return the index instead of the value.
import java.util.Arrays;
public class SmallestLoop {
// Starts from the first element instead of a guessed "large" value
static int findSmallest(int[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("array is empty");
}
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
public static void main(String[] args) {
int[][] inputs = {{12, -5, 48, 7}, {9, 9, 9}, {2147483647}, {}};
for (int[] arr : inputs) {
try {
System.out.println(Arrays.toString(arr) + " -> " + findSmallest(arr));
} catch (IllegalArgumentException e) {
System.out.println(Arrays.toString(arr) + " -> " + e.getMessage());
}
}
}
}
[12, -5, 48, 7] -> -5 [9, 9, 9] -> 9 [2147483647] -> 2147483647 [] -> array is empty
Time complexity: O(n) · Space complexity: O(1).
Approach 2: IntStream.min()
Compact and safe for empty input, because the Optional forces you to decide what to do when there is no value.
import java.util.Arrays;
public class SmallestStream {
// IntStream.min() returns an empty OptionalInt for an empty array
static int findSmallest(int[] arr) {
return Arrays.stream(arr)
.min()
.orElseThrow(() -> new IllegalArgumentException("array is empty"));
}
public static void main(String[] args) {
int[][] inputs = {{12, -5, 48, 7}, {9, 9, 9}, {2147483647}, {}};
for (int[] arr : inputs) {
try {
System.out.println(Arrays.toString(arr) + " -> " + findSmallest(arr));
} catch (IllegalArgumentException e) {
System.out.println(Arrays.toString(arr) + " -> " + e.getMessage());
}
}
}
}
[12, -5, 48, 7] -> -5 [9, 9, 9] -> 9 [2147483647] -> 2147483647 [] -> array is empty
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Single Loop | O(n) | O(1) |
| IntStream.min() | O(n) | O(1) |
Here n is the number of elements. Both approaches look at every element once and keep a single running minimum.
Interview Tips and Recommended Approach
- Initialise from the array, not from a constant.
min = arr[0]is always correct; a guessed starting value such as1000is not. - If you do use a constant, use
Integer.MAX_VALUE. Then explain how you tell "the minimum is MAX_VALUE" apart from "the array was empty". - Handle the empty array explicitly. Accessing
arr[0]on an empty array throwsArrayIndexOutOfBoundsException. - Offer a combined pass. If the interviewer asks for both the minimum and the maximum, track both in the same loop.
Recommended Approach: Single Loop
Why: it runs in one pass with constant memory and never depends on a guessed starting value. It is also the easiest version to extend to the minimum's index or to a combined min-max search.