How to find the smallest element in an array in Java

Hey, fellow code crafters! In this tutorial, you are going to learn the Java program to find the smallest element in an array. This can be achieved in two approaches.

Java Program to find the smallest element in an array

Approach 1: Considering a variable min as the initial element and updating it.

Consider a variable min which holds the value of the first element. Using a loop the elements of the array are compared with min. If any value of the elements is less than min, then it will be stored in min. The updated min value is again compared with the remaining elements of the array. The loop will be repeated until it reaches the last element.

Ex:

An example array with 5 elements in it.

Consider the above array in which the initial min value is 23. In the first iteration, the min value is compared with 45. Since 45 is greater than 23, min will not update. In the second iteration, the min is compared with 17. Since 17 is less than 23 min will be updated as 17. This process continues until it reaches the last element.

import java. util.*;
public class Codespeedy
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array: ");
        int N=sc.nextInt();
        int arr[]=new int[N];
        System.out.println("Enter elements in the array");
        for(int i=0;i<=N-1;i++)
        {
            arr[i]=sc.nextInt();
        }
        int min=arr[0];
        for(int i=0;i<=N-1;i++)
        {
            if(arr[i]<min)
            {
                min=arr[i];
            }
        }
        System.out.println("The minimum element in the array is "+min);
    }
}
Output:
Enter the size of the array: 
5
Enter elements in the array
23
45
17
76
34
The minimum element in the array is 17

Approach 2: Considering min as an Integer.MAX_VALUE

In this approach, we consider min as an Integer.MAX_VALUE i.e. min is equal to the maximum possible value for int’ datatype (2147483645). Then this min value is compared with the remaining elements within a loop.

Ex:

Another example of an array with 5 elements.

Since the min value is 2147483647 (maximum value of int datatype), it will be compared with 45 in the first iteration. Since 45 is less than 2147483647, min will be updated to 45. Then in the second iteration min is again compared with 23. Since 23 is less than 45, min will be updated with 23. This will continue till the loop reaches the last element.

import java. util.*;
public class Codespeedy
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array: ");
        int N=sc.nextInt();
        int arr[]=new int[N];
        System.out.println("Enter elements in the array");
        int min= Integer.MAX_VALUE;
        for(int i=0;i<=N-1;i++)
        {
            arr[i]=sc.nextInt();
            if(arr[i]<min)
            {
                min=arr[i];
            }
        }
        System.out.println("The minimum element is "+min);
    }
}
Output:
Enter the size of the array: 
5
Enter elements in the array
45
23
78
22
16
The minimum element is 16

Hope this tutorial was helpful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *