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

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

In this article we are going to see how we can find the second smallest element in an array using Java Programming language.

Java Program to Find the Second Smallest Number in an Array

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 second smallest element in an array.

Method-1: Java Program to Find the Second Smallest Number in an Array By Comparing Array 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.
  • Print the second smallest element.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 2, 34, 20, 54, 6};

        // 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;
        }

        // Prints the array elements
        System.out.println("The array elements are : "+Arrays.toString(arr));
        
        // Prints the second smallest element
        System.out.println("The second smallest element of the array is : "+secondSmall);
    }
}



Output:

The array elements are : [12, 2, 34, 20, 54, 6]
The second smallest element of the array is : 6

Method-1: Java Program to Find the Second Smallest Number in an Array By Using Sorting (Array.sort())

Approach:

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

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 2, 34, 20, 54, 6};
        
        // Sorts the array in ascending order
        Arrays.sort(arr);

        // Prints the array elements
        System.out.println("The array elements are : "+Arrays.toString(arr));
        
        // Prints the second smnallest element
        System.out.println("The second smallest element of the array is : "+arr[1]);
    }
}


Method-3: Java Program to Find the Second Smallest Number in an Array By Using Array List 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 second element.

Program:

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

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        Integer arr[] = {12, 2, 34, 20, 54, 6};
        
        // Converts the array into a list
        List<Integer> list=Arrays.asList(arr);
        // Sorts the array in ascending order  
        Collections.sort(list);  

        // Prints the array elements
        System.out.println("The array elements are : "+Arrays.toString(arr));
        
        // Prints the second smallest element
        System.out.println("The second smallest element of the array is : "+list.get(1));
    }
}
Output:

The array elements are : [2, 6, 12, 20, 34, 54]
The second smallest element of the array is : 34

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: