Last Updated on May 17, 2023 by Prepbytes
In Java, finding the smallest element in an array involves iterating through each element and keeping track of the smallest value found so far. The process can be done using a loop, comparing each element to the current minimum value, and updating the minimum value as needed. Once all elements have been compared, the minimum value will be the smallest element in the array.
For example, if we have an array like {5,2,8,1,6} then the minimum element in the array is 1.
How to Find the Minimum Element in the Array
We have different ways to find the minimum element in the array.
Method 1: Using Iteration
The algorithm steps are as follows:
- Step 1: Create a variable called min and set the array’s first member as its starting value.
- Step 2: Check after running a loop from index 1 to N and set min = arr[i] if arr[i] > min.
- Step 3: After completing the iteration, print the minimum element.
Code Implementation
class Main { public static void main(String args[]) { int arr[] = {12, 13, 12, 10, 34, 10}; int min = arr[0]; for(int i=0; i<arr.length; i++) { if(min > arr[i]) { min = arr[i]; } } System.out.println("The minimum element is" + min); } }
Output
The minimum element is 10
Method 2: Using Recursion
The algorithm steps are as follows:
- Step 1: Build a recursive function like getmin(int arr[, int n]).
- Step 2: Basis: If (n==1), return arr[0].
- Step 3: Otherwise, return min(arr[n-1], getmin(arr, n-1)).
Code Implementation
import java.util.Scanner; import java.util.*; public class Main { static int getmin(int arr[], int n){ if(n==1) return arr[0]; return Math.min(arr[n-1], getmin(arr, n-1)); } public static void main(String args[]) { int arr[] = {12, 13, 12, 10, 34, 10}; int n = arr.length; System.out.print(getmin(arr, n)); } }
Output
10
Conclusion
In conclusion, we may initialize a variable to the first element of the array and then loop over the other members, comparing each one to the current smallest value and updating the variable as necessary to determine the smallest element in an array in Java. The variable will then contain the array’s lowest value, which may be printed out or utilized in further computations, once the loop has completed.
Frequently Asked Questions
Q1. Can we use any data type for the elements of the array?
Ans. Yes, we can use any data type for the elements of the array, including integers, floats, doubles, or objects. However, the comparison logic in the loop would need to be adjusted accordingly.
Q2. What if the array is empty?
Ans. If the array is empty, the program would throw an exception when trying to access the first element of the array. To handle this case, we could add a check at the beginning of the program to see if the array is empty, and then handle it accordingly.
Q3. What if there are multiple elements with the same smallest value?
Ans. If there are multiple elements with the same smallest value, the program would only find one of them. To find all occurrences of the smallest value, we would need to modify the program to keep track of the indices or count of all the smallest elements.
Q4. Can we use Java built-in functions to find the smallest element?
Ans. Yes, there are built-in functions in Java that can be used to find the smallest element in an array, such as Arrays.stream(arr).min().getAsInt() for integer arrays. However, implementing the logic manually using a loop can be a useful exercise for learning and practicing programming skills.