Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type.
Example:
arr1[] = {2 , -1 , 9 , 10}
output : -1
arr2[] = {0, -10, -13, 5}
output : -13
We need to find and print the smallest value element of an array in this program.
- By maintaining a min element and updating it while traversing over the whole array if we encounter a number smaller than min.
- By sorting an array and printing the 0th index element of the array after sorting.
Approach 1: Maintaining a min element and updating it while traversing over the whole array if we encounter a number smaller than min.
Java
public class FindSmallestElementInArray {
public static void main(String[] args)
{
int [] initializedArray
= new int [] { 25 , 110 , 74 , 75 , 5 };
System.out.println( "Given array " );
for ( int i = 0 ; i < initializedArray.length; i++) {
System.out.println(initializedArray[i]);
}
int minValue = initializedArray[ 0 ];
for ( int i = 0 ; i < initializedArray.length; i++) {
if (initializedArray[i] < minValue)
minValue = initializedArray[i];
}
System.out.println(
"Smallest element present in given array: "
+ minValue);
}
}
|
Output
Given array
25
110
74
75
5
Smallest element present in given array: 5
Time Complexity: O(n)
Space Complexity: O(1)
Approach 2: By sorting an array and printing the 0th index element of the array after sorting.
Java
import java.util.*;
public class FindSmallestElementInArray {
public static void main(String[] args)
{
int [] initializedArray = new int [] { 25 , 110 , 74 , 75 , 5 };
System.out.println( "Given array " );
for ( int i = 0 ; i < initializedArray.length; i++) {
System.out.println(initializedArray[i]);
}
Arrays.sort(initializedArray);
int minValue = initializedArray[ 0 ];
System.out.println(
"Smallest element present in given array: "
+ minValue);
}
}
|
Output
Given array
25
110
74
75
5
Smallest element present in given array: 5
Time complexity: O(NlogN) Since the time taken for sorting is NlogN, where there are N elements of the array
Space complexity: O(1)
Approach 3: Using Collections.min() and ArrayList
Java
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args)
{
int [] initializedArray
= new int [] { 25 , 110 , 74 , 75 , 5 };
ArrayList<Integer> al = new ArrayList<>();
System.out.println( "Given array " );
for ( int i = 0 ; i < initializedArray.length; i++) {
System.out.println(initializedArray[i]);
al.add(initializedArray[i]);
}
System.out.println(
"Smallest element present in given array: "
+ Collections.min(al));
}
}
|
Output
Given array
25
110
74
75
5
Smallest element present in given array: 5
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Sep, 2022
Like Article
Save Article