Arrays are one of the most important topics in Java programming. Whether you are learning Core Java, Data Structures and Algorithms (DSA), or preparing for interviews, you will frequently encounter problems related to arrays.
One of the most common beginner questions is: How do we find the minimum element in an array?
Although the solution is simple, understanding the logic behind it is very important because the same pattern is used in many other programming problems.
Imagine you have exam scores:
78, 65, 90, 45, 88
If someone asks you to find the lowest score, you naturally compare the numbers one by one.
Programming follows the same approach.
We assume the first element is the minimum value and then compare every other element with it.
If we find a smaller value, we update the minimum variable.
Finding the minimum value is a basic operation used in many real applications.
| Application | Use Case |
|---|---|
| E-commerce | Find cheapest product |
| Weather App | Find minimum temperature |
| School Software | Find lowest marks |
| Analytics | Find smallest value in dataset |
Let's solve the problem step by step.
public class Main {
public static void main(String[] args) {
int[] arr = {12, 5, 9, 2, 18, 7};
int min = arr[0];
for(int num : arr) {
if(num < min) {
min = num;
}
}
System.out.println("Minimum Element: " + min);
}
}
First, we create an array containing six numbers.
int[] arr = {12, 5, 9, 2, 18, 7};
Next, we assume the first element is the minimum.
int min = arr[0];
Now we iterate through every element.
for(int num : arr)
Whenever we find a smaller value, we update min.
if(num < min){
min = num;
}
Finally, we print the minimum value.
Minimum Element: 2
The smallest number in the array is 2, so the program prints 2.
This may produce incorrect results if all values are positive.
This will find the maximum element instead.
The result will remain the first element.
The algorithm visits every element exactly once.
Time Complexity: O(n)
Space Complexity: O(1)
This is the most efficient approach because every element must be checked at least once.
The smallest value present inside an array.
Because it guarantees a valid starting value from the array.
Yes. The logic works for both positive and negative values.
O(n).
O(1).
Yes. Both normal for loop and foreach loop work.
That element itself becomes the minimum value.
The same value will be returned as minimum.
Yes, but beginners should first understand the loop approach.
Yes. It is one of the most common array interview questions.
Finding the minimum element in an array is one of the first array problems every Java developer should learn.
The idea is simple:
Although this problem looks easy, it teaches an important programming pattern that appears in many DSA and interview questions. Once you understand this concept, solving array-based problems becomes much easier.
After learning minimum element, practice these problems:
Mastering these array problems will build a strong foundation for Data Structures and Algorithms interviews.