Program For Finding Second Smallest Element Of Array

Description

To find second smallest element, first find the smallest element.
To find smallest element, first declare a largest element.
min = Integer.MAX_VALUE
Compare each element with this. If element smaller than this is found,
update min value. Continue this process until minimum element found.

Now for finding second smallest, again repeat the same process to find 
smallest element but also check it should not be equal to minimum 
element(min).

C/C++

/* C program to print second smallest element of the array*/
//Save it as SecondSmallestElementArray.c

#include<stdio.h>
#include<limits.h>

int main(){

    int i, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    //Declaring array
    int arr[n];

    printf("Enter the elements of the array : ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

    //Declaring minimum element
    int minimum = INT_MAX;

    //Comparing with each element and find minimum element
    for(i=0;i<n;i++){
        if(arr[i]<minimum){
            minimum = arr[i];
        }
    }

    printf("The minimum value is : %d",minimum);

    int second_minimum = INT_MAX;


    //Finding Second smallest element.
    //Comparing with each element and also checking it is not equal to minimum
    for(i=0;i<n;i++){
        if(arr[i]<second_minimum && arr[i]!=minimum){
            second_minimum = arr[i];
        }
    }

    printf("\nThe second minimum value is : %d",second_minimum);

    return 0;
}

Output

Output

Java

/*Java program to print second smallest element of the array*/
//Save it as SecondSmallestElementArray.java

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

public class SecondSmallestElementArray {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        //Declaring array
        int arr[] = new int[n];
        
        System.out.println("Enter the array elements : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        //Declaring minimum element
        int minimum = Integer.MAX_VALUE;
        
        //Comparing with each element and find minimum element
        for(int i=0;i<n;i++) {
            if(arr[i]<minimum) {
                minimum = arr[i];
            }
        }
        
        System.out.println("The minimum value is : "+minimum);
        
        int second_minimum = Integer.MAX_VALUE;
        
        //Finding Second smallest element.
        //Comparing with each element and also checking it is not equal to minimum
        for(int i=0;i<n;i++) {
            if(arr[i]<second_minimum && arr[i]!=minimum) {
                second_minimum = arr[i];
            }
        }
        
        System.out.println("The second minimum value is : "+second_minimum);
    }
}

Output

Output

Related programs

1) Program for finding Second Largest Element Of Array
2) Find the Average of Elements of Array
3) Find Smallest element and it’s position of an Array
4) Program to swap maximum and minimum element of Array
5) Program to Find the smallest missing number
6) Program to Find missing odd number in first n odd number
7) Program to Find K largest elements from array
8) Remove Duplicate Elements From Array
9) Program to Print How Many Numbers Smaller than Current Number
10) Program to find maximum and second maximum if elements of array is space separated input
Share Me

Leave a Reply