How to find the smallest number in an array java – Java Program to Find the Third Smallest Number in an Array

How to find the smallest number in an array java: In the previous article, we have seen Java Program to Find the Third Largest Number in an Array

In this article we are going to see how we can find the third smallest element in an array.

Java Program to Find the Third Smallest Number in an Array

How to find smallest number in array java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the third smallest element in an array.

Method-1: Java Program to Find the Third Smallest Number in an Array By Comparing Elements

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Create a variable and store the first element of the array in it.
  • Compare the variable with the whole array to find and store the Smallest element.
  • Repeat the above step for the array elements except the Smallest element.
  • Repeat the steps again to find out the third smallest element and print it.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 2, 34, 20, 54, 6};
        
        // Prints the array elements
        printArray(arr);
        
        // Initializing the first element of the array to Small
        int small=arr[0];        
        
        // Compares all the element to find out the Smallest one
        for(int i:arr)
        {
            if(small>i)
                small=i;
        }

        // Initializing the first element of the array to secondSmall
        int secondSmall=arr[0];        
        
        // Compares all the element to find out the second Smallest one
        for(int i:arr)
        {
            if(secondSmall>i&&i!=small)
                secondSmall=i;
        }

        int thirdSmall=arr[0];        
        
        // Compares all the element to find out the third Smallest one
        for(int i:arr)
        {
            if(thirdSmall>i&&i!=small&&i!=secondSmall)
                thirdSmall=i;
        }

        
        // Prints the third smallest element
        System.out.println("The third smallest element of the array is: "+thirdSmall);
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        System.out.println("The array elements are");
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}



Output:

The array elements are
12 2 34 20 54 6 
The third smallest element of the array is: 12

Method-2: Java Program to Find the Third Smallest Number in an Array By Using Sorting

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Use function Arrays.sort() to sort the array in ascending order.
  • Print the third element.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        Integer arr[] = {12, 2, 34, 20, 54, 6};
        
        // Prints the array elements
        printArray(arr);
        
        // Sorts the array in ascending order
        Arrays.sort(arr);
        
        // Prints the third smallest element
        System.out.println("The third smallest element of the array is: "+arr[2]);
    }

    // Function to print the array
    static void printArray(Integer arr[])
    {
        System.out.println("The array elements are");
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements are
12 2 34 20 54 6 
The third smallest element of the array is: 12

Method-3: Java Program to Find the Third Smallest Number in an Array By Using ArrayList and collections

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Convert the array elements into a list.
  • Use the Collection.sort function to sort the list in ascending order.
  • Print the third element.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        Integer arr[] = {12, 2, 34, 20, 54, 6};
        
        // Prints the array elements
        printArray(arr);
        
        // Converts the array into a list
        List<Integer> list=Arrays.asList(arr);
        // Sorts the array in ascending order  
        Collections.sort(list);
        
        // Prints the third smallest element
        System.out.println("The third smallest element of the array is: "+list.get(2));
    }

    // Function to print the array
    static void printArray(Integer arr[])
    {
        System.out.println("The array elements are");
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements are
12 2 34 20 54 6
The third smallest element of the array is: 12

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Testy yourself if you grasp the content given above:

  1. Find smallest number in array java using methods?
  2. How to find the smallest number in an array javascript?
  3. Find smallest number in array java 8?
  4. Find smallest number in arraylist java?
  5. Find smallest number in array java without sorting?
  6. How to find smallest number in java?
  7. Find smallest number in array java using scanner?
  8. Find the smallest number in an array python?
  9. How to find smallest number in java?
  10. Find smallest number in array java 8?
  11. Find smallest number in arraylist java?
  12. Write a program to find the smallest even number in a given array in java/
  13. Find smallest number in array java using methods?
  14. How to find the smallest number in an array javascript?
  15. How to find the smallest number in an array java?
  16. How to find the second smallest number in an array java?
  17. How to find the index of the smallest number in an array java?
  18. How to find the largest and smallest number in an array java?
  19. How to find the smallest number in an arraylist java?
  20. How to find the smallest number in a list java?
  21. How to find the largest and smallest number in an array javascript?
  22. How to find closest number in an array java?
  23. How to find the lowest number in java?
  24. How to find smallest number in arraylist?

Related Java Programs: