Java Program to Print the Smallest Element in an Array Last Updated : 13 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } OutputGiven 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); } } OutputGiven 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)); } } OutputGiven array 25 110 74 75 5 Smallest element present in given array: 5 Comment More infoAdvertise with us Next Article Java Program to Print the Smallest Element in an Array priyarajtt Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Array-Programs +1 More Practice Tags : Java Similar Reads Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print 2 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO 2 min read Java Program to Return the Largest Element in a List Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions. Input : List = [5, 3, 234, 114, 154] Output : 234 Input : List = {10, 20, 4} Output : 20Approach 1: Using a ForEach Loop Create L 3 min read Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We 4 min read Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr 2 min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Java Program to Print All the Repeated Numbers with Frequency in an Array The frequency of an element in an array is the count of the occurrence of that particular element in the whole array. Given an array that may contain duplicates, print all repeated/duplicate elements and their frequencies. Below is the discussion of this program by two approaches: Using a counter ar 4 min read Like