Program to find the second smallest element in an array | faceprep

Finding the second smallest element in an array can be done in 3 different ways. They are:

  • Method 1: By sorting the array in ascending order and then displaying the second element.
  • Method 2: By traversing the array twice. In the first traversal find the smallest element (x) and in the second traversal, skip x and find the next smallest element which is >x.
  • Method 3: By traversing the array and storing the smallest element as and when we encounter it.

Method 1 to find the second smallest element in an array

An easier approach to finding the second smallest element in an array will be sorting the array completely and then returning the second element in the array.

Algorithm

  • Declare an array and input the array elements.
  • Sort the array (sort(arr, arr+n)).
  • Display the second element from the array.

second smallest element in an array

#include <stdio.h>
void sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main()
{
int n,i;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
sort(arr, n);
printf("\nThe second smallest element is %d \n",arr[1]);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
cout << "Enter the number of elements : ";
cin >> n;
int arr[n];
cout << "\nInput the array elements : ";
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, arr+n);
cout << "\nThe second largest element is " << arr[1];
cout << endl;
return 0;
}

Method 2 to find the second smallest element in an array

The second smallest element can be also found by traversing the array two times. In the first traversal, the smallest element (first_smallest) in the array is found and in the second traversal, the smallest element other than the first_smallest element is found.

Algorithm

  • Declare an array and input the array elements.
  • Find the smallest element (first_smallest) in the array in the first traversal.
  • Find the smallest element (second_smallest)  by skipping the first_smallest element.
  • Display second_smallest.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
int n,i;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
int min = INT_MAX;
int second_min = INT_MAX ;
if (n < 2)
{
printf("\nInvalid Input");
}
for(i = 0; i < n; i++)
{
if(arr[i] < min)
{
second_min = min;
min = arr[i];
}
}
for(i = 0; i < n; i++)
{
if(arr[i] < second_min && arr[i] != min)
{
second_min = arr[i];
}
}


printf("\nThe second smallest element is %d \n", second_min);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int min = INT_MAX;
int i,n = 5;
cout << "\nEnter the number of elements : ";
cin >> n;
int arr[n];
cout << "\nInput the array elements : ";
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
int second_min = INT_MAX ;
if (n < 2)
{
cout <<"\nInvalid Input";
}
for(i = 0; i < n; i++)
{
if(arr[i] < min)
{
second_min = min;
min = arr[i];
}
}
for(i=0;i<n;i++)
{
if(arr[i] < second_min && arr[i] != min)
{
second_min = arr[i];
}
}
cout << "\nThe second smallest element is " << second_min;
cout << endl;
return 0;
}

Method 3 to find the second smallest element in an array

The most efficient method will be finding the second smallest element by traversing the array once.

Algorithm

  • Declare an array and input the array elements.
  • Initialize first_smallest = second_smallest = INTMAX;
  • Start traversing the array and assign the smallest element to first_smallest.
  • In the same traversal, if the next element is smaller than first_smallest, make that as first_smallest and make first_smallest as second_smallest.
  • Display second_smallest.

second smallest element in an array

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main()
{
int n,i;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
int min = INT_MAX;
int second_min = INT_MAX ;
if (n < 2)
{
printf("\nInvalid Input");
}
for(i = 0; i < n; i++)
{
if(arr[i] < min)
{
second_min = min;
min = arr[i];
}
else if(arr[i] < second_min && arr[i] != min)
{
second_min = arr[i];
}
}
printf("\nThe second smallest element is %d \n", second_min);
return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int n,i;
    cout << "\nEnter the number of elements :  ";
    cin >> n;
    int arr[n];
    cout << "\nInput the array elements : ";
    for(i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int min = INT_MAX;
    int second_min = INT_MAX ;
    if (n < 2)
    {
        cout << "\nInvalid Input";
    }
    for(i = 0; i < n; i++)
    {
        if(arr[i] < min)
        {
            second_min = min;
            min = arr[i];
        }
        else if(arr[i] < second_min && arr[i] != min)
        {
            second_min = arr[i];
        }
    }
    cout << "\nThe second smallest element is "<< second_min;
    cout << endl;
    return 0;
}

Output Test case

Input: 1 2 3 4 5
Output: 2

Output:

second smallest element in an array