# Second smallest element in an array

## The second smallest element in an array

Here we will find about the second smallest of the array elements.

For this program, we need to have a sorted array. so we easily extract the second element of the array as the smallest element.

`Example:Input:                     2 4 5 1 7 6Sorted array               1 2 4 5 6 7Second Smallest element    2`

Now we will see the working and the java implementation to find the second smallest number.

### Working

Step 1. Initialize arrays.

Step 2. Declare the scanner class for taking input.

Step 3. take array size from the user.

Step 4. Take an element of the array from the user.

Step 5.  Print the return value of the function secondSmall.

secondSmall(int a[], int size)

Step 1. Initialise one variable with zero.

Step 2.  Declare outer loop from zero to size to select a single element in the array.

Step 3.  Declare the inner loop from 0 to size to compare array element.

Step 4.  Return a[1], it returns the second index value.

`#include <stdio.h>  int main()  {    int n, i , j, temp;    printf("enter size : ");    scanf("%d",&n);    int arr[n];    printf("enter elements : ");    for(i=0; i<n; i++)    {        scanf("%d",&arr[i]);    }    for(i=0; i<n; i++)    {        for(j=i+1; j<n; j++)        {           if(arr[i]>arr[j])            {                temp = arr[i];                arr[i] = arr[j];                arr[j] = temp;            }        }    }    printf("second smallest element is : %d",arr[1]);    return 0;}  `
`import java.util.Scanner;public class Main{    public static int SecondSmall(int a[],int size)     {        int temp=0;        //perform sorting         for(int i=0;i<size;i++)        {            for(int j=0;j<size;size++)            {                  if(a[i]>a[j])                  {                    temp=a[i];                    a[i]=a[j];                    a[j]=temp;                  }             }        }       //return second index value       return a[1];    }    public static void main(String[] args)     {          int j=0;          int[] arr=new int[40];          //decklare scanner class          Scanner sc=new Scanner(System.in);          System.out.println("enter size");          int size=sc.nextInt();          System.out.println("enter element");          for(int i=0;i<size;i++)                arr[i]=sc.nextInt();             //print second smallest value with function call          System.out.print("Second smallest number "+SecondSmall(arr,size));     }}`

### Output

`enter size4enter element2341second smallest number 2`