Java Code for finding the Smallest Element in an Array

Smallest Element in an Array using Java

Here, in this page we will discuss the program to find the smallest element in an array using java. We are given with an array and we need to print the smallest element among the given elements of the array.

Java Program to find the smallest element in the array(1)

We will discuss different methods to find the smallest element among the elements of the given array.

Methods Covered :

  • Method 1: Using Iteration
  • Method 2: Top-down Recursion.
  • Method 3: Bottom-up Approach

Method 1 :

Objective: Find the Smallest element in array Java

  • Declare a variable say min and initialize it with array first element.
  • Run a loop from index 1 to N, and check
  • If arr[i]<min, then set min = arr[i]
  • After complete iteration print min.

Java Program for finding the smallest number in an array

Run
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {

     int arr[] = {12, 13, 1, 10, 34, 10};

     int min = arr[0];

     for(int i=0; i<arr.length; i++) { if(min > arr[i])
         {
            min = arr[i];
         }

     }

     System.out.print(min); 
  }
}

Output

1

Method 2 (Using Recursion) :

  • Create a recursive function say getmin(int arr[], int n)
  • Base Condition : if(n==1) return arr[0]
  • Otherwise, return min(arr[n-1], getmin(arr, n-1))
Smallest Element

Method 2 : Java Code

Run
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, 1, 10, 34, 10};
      int n = arr.length;
      System.out.print(getmin(arr, n)); 
   }
}

Output

1

Method 3 (Using Bottom up recursion)

Call a function : minimum(int arr[], int i, int end)

  • With initial call up values as minimum(arr, 0, end)
    • Initially passing end as the index of last array element
    • Initially passing ‘i’ as 0
  • Recursively reach iteration where reading 2nd last element
  • Find smaller between 2nd last and last array elements
  • And return the result to min value of the previous recursive iteration
  • In each of the remaining recursive callups find the smaller b/w current array index element and current min value
  • Pass final min value from last recursive callup to main and print

Method 3 : Java Code

Run
import java.util.Scanner;
import java.util.*;

public class Main
{ 
   static int getmin(int arr[], int i, int end)
   {
      int min;

      if(i == end-1)
         return (arr[i] < arr[i + 1]) ? arr[i] : arr[i + 1];

      min = getmin(arr, i + 1, end);

      return (arr[i] < min) ? arr[i] : min;
   }

   public static void main(String args[])
   {

     int arr[] = {12, 13, 1, 10, 34, 10};
     int end = arr.length-1;
     System.out.print(getmin(arr, 0, end)); 
   }
}

Output

1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Sorting elements of an array by frequency : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | JavaPython
  • Counting Distinct Elements in an Array : C | C++ | JavaPython
  • Finding  Repeating elements in an Array : C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java | Python
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java | Python
  • Find all Symmetric pairs in an array : C | C++ | Java | Python
  • Find maximum product sub-array in a given array : C | C++ | Java | Python
  • Finding Arrays are disjoint or not : C | C++ | Java | Python
  • Determine Array is a subset of another array or not : C | C++ | Java | Python
  • Determine can all numbers of an array be made equal : C | C++ | Java | Python
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
  • Sort an array according to the order defined by another array : C | C++ | Java | Python
  • Replace each element of the array by its rank in the array : C | C++ | Java | Python
  • Finding equilibrium index of an array : C | C++ | JavaPython
  • Rotation of elements of array- left and right : C | C++ | JavaPython
  • Block swap algorithm for array rotation : C | C++ | JavaPython
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python
  • Balanced Parenthesis Problem : C | C++ | Java | Python

2 comments on “Java Code for finding the Smallest Element in an Array”


  • Gyanendra

    //By usiing Recursion
    import java.util.*;

    public class Main
    {
    public static void main(String[] args) {
    int[] arr={3,5,6,8,3,9};
    System.out.println(find(arr,0));
    }

    public static int find(int[] arr,int n){
    if(n==arr.length){
    return 0;
    }

    int val=find(arr,n+1);

    if(val>arr[n]){
    return val;
    }
    return arr[n];
    }
    }


  • Srinibash

    import java.util.Scanner;
    public class p1 {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int x =Integer.MAX_VALUE;
    System.out.println(“Enter Size of Array”);
    int size = sc.nextInt();
    int[] arr = new int[size];
    System.out.println(“Enter “+size+” Elements to Store inside Array”);
    for(int i = 0 ; i< size ; i ++){
    arr[i]= sc.nextInt();
    }
    for(int i = 0 ; i<arr.length; i++){

    if(arr[i] < x){
    x= arr[i];
    }
    }
    System.out.println("Smallest Element inside this Array is "+x);
    }
    }