Saturday, September 27, 2025
HomeLanguagesJavaJava Program to Print the Smallest Element in an Array

Java Program to Print the Smallest Element in an Array

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.

  1. By maintaining a min element and updating it while traversing over the whole array if we encounter a number smaller than min.
  2. 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




// Java program to print the smallest element of the array
 
public class FindSmallestElementInArray {
    public static void main(String[] args)
    {
 
        // Either we can initialize array elements or can
        // get input from user. Always it is best to get
        // input from user and form the array
        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]);
        }
 
        // Initialize minValue with first element of array.
        int minValue = initializedArray[0];
 
        // Loop through the array
        for (int i = 0; i < initializedArray.length; i++) {
 
            // Compare elements of array with minValue and
            // if condition true, make minValue to that
            // element
 
            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




// Java program to print the smallest element of the array
 
import java.util.*;
 
public class FindSmallestElementInArray {
    public static void main(String[] args)
    {
       
        // we can initialize array elements
        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]);
        }
 
        // sort the array
        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




// Java program to print the smallest element of the array
 
import java.lang.*;
import java.util.*;
 
public class Main {
 
  public static void main(String[] args)
    {
 
        // Either we can initialize array elements or can
        // get input from user. Always it is best to get
        // input from user and form the array
        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]);
            // adding elements of array to arrayList.
            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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32322 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6804 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS