0
0
JavaProgramBeginner · 2 min read

Java Program to Find Smallest Element in Array

To find the smallest element in an array in Java, use a loop to compare each element with a variable holding the smallest value, updating it when a smaller element is found, like int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) min = arr[i]; }.
📋

Examples

Input[3, 5, 1, 2, 4]
Output1
Input[10, 9, 8, 7, 6]
Output6
Input[-1, -5, -3, 0]
Output-5
🧠

How to Think About It

To find the smallest element, start by assuming the first element is the smallest. Then check each other element one by one. If you find an element smaller than your current smallest, update your smallest value. At the end, the smallest value you have is the smallest element in the array.
📐

Algorithm

1
Start with the first element as the smallest value.
2
Go through each element in the array from the second to the last.
3
Compare the current element with the smallest value.
4
If the current element is smaller, update the smallest value.
5
After checking all elements, return the smallest value.
💻

Code

java
public class SmallestElement {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 2, 4};
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("Smallest element is: " + min);
    }
}
Output
Smallest element is: 1
🔍

Dry Run

Let's trace the array [3, 5, 1, 2, 4] through the code to find the smallest element.

1

Initialize min

min = 3 (first element)

2

Compare with second element

arr[1] = 5; 5 < 3? No; min stays 3

3

Compare with third element

arr[2] = 1; 1 < 3? Yes; min updated to 1

4

Compare with fourth element

arr[3] = 2; 2 < 1? No; min stays 1

5

Compare with fifth element

arr[4] = 4; 4 < 1? No; min stays 1

6

Result

Smallest element found is 1

IterationCurrent Elementmin Value
Start33
153
211
321
441
💡

Why This Works

Step 1: Initialize min

We start by assuming the first element is the smallest using min = arr[0].

Step 2: Compare elements

We check each element with if (arr[i] < min) to find if there is a smaller value.

Step 3: Update min

If a smaller element is found, we update min to that element.

Step 4: Final result

After checking all elements, min holds the smallest element in the array.

🔄

Alternative Approaches

Using Arrays.stream()
java
import java.util.Arrays;
public class SmallestElementStream {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 2, 4};
        int min = Arrays.stream(arr).min().getAsInt();
        System.out.println("Smallest element is: " + min);
    }
}
This uses Java 8 streams for a concise solution but may be less clear for beginners.
Sorting the array
java
import java.util.Arrays;
public class SmallestElementSort {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 2, 4};
        Arrays.sort(arr);
        System.out.println("Smallest element is: " + arr[0]);
    }
}
Sorting is simple but less efficient if only the smallest element is needed.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so it runs in linear time, O(n), where n is the number of elements.

Space Complexity

It uses only a few variables and no extra arrays, so space complexity is constant, O(1).

Which Approach is Fastest?

The loop method is fastest for this task. Sorting takes O(n log n), and streams add some overhead.

ApproachTimeSpaceBest For
Loop through arrayO(n)O(1)Finding smallest element efficiently
Arrays.stream().min()O(n)O(1)Concise code with Java 8+
Sorting arrayO(n log n)O(1)When array needs sorting anyway
💡
Always initialize your smallest value with the first element of the array before looping.
⚠️
A common mistake is initializing the smallest value to zero or a fixed number instead of the first array element.