Signup/Sign In
PUBLISHED ON: MARCH 6, 2021

C Program To Find the Second Smallest and Second Largest element in an Array

In this tutorial, we will learn how to find the second smallest and the second-largest element in an array. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

The second smallest and the second-largest element in an array can be found in the following three ways:

Method 1: By sorting the elements in descending order and then displaying the second smallest and second largest element in the array.

Method 2: By traversal method. In the first traversal, the smallest/largest element is found. In the second traversal, the smallest/largest element is skipped and the next smallest/largest element is found.

Method 3: In a single traversal, finding the second smallest/largest element.

Program 1: To Find the Second Smallest and Second Largest Element in an Array

This the simplest approach to find the second largest and second smallest element in an array. Firstly, the array is sorted in descending order and then the second smallest and second largest element is printed.

Algorithm

  1. Start.
  2. Declare an array.
  3. Ask the user to initialize the array.
  4. Sort the array in descending order by comparing and swapping.
  5. Print the second smallest and second largest element i.e., the second and second last index element.
  6. Stop

Below is the code for the same.

#include <stdio.h>

int main()
{
    int n;
    printf("Enter the number of elements:");
    scanf("%d",&n);
    printf("Enter the array elements :");
    int a[n];                    //Array Declaration
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0;i<n;i++)         //Sorting Array
    {
        int temp;
        for(int j=i+1; j<n ;j++)
        {
            if(a[i]<a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("The second smallest element is %d",a[n-2]);   //Accessing the smallest element
    printf("\n");
    printf("The second largest element is %d",a[1]);      //Accessing the largest element
    return 0;
}


Enter the number of elements: 5
Enter the array elements : 3 5 8 9 7
The second smallest element is 5
The second largest element is 8

Program 2: To Find the Second Smallest and Second Largest Element in an Array

In order to find the second smallest and second largest element in an array, we can traverse the array twice. We will find the smallest and largest element in the first traversal and then in the second traversal, the second smallest and second largest element is found out.

Algorithm

  1. Start
  2. Declare an array.
  3. Ask the user to initialize the array
  4. Find the first smallest and first largest in the array in the first traversal.
  5. Find the second smallest and second largest element in the array in the next traversal.
  6. Display the second smallest and second largest element.
  7. Stop

Below is the code for the same.

#include <stdio.h>

int secondSmallest(int a[],int n)
{
    int min = a[0];
    int secondMin = a[1] ;
    for(int i = 0; i < n; i++)     //First-time Array Traversal
    {
       if(a[i] < min)
       {
          secondMin = min;
          min = a[i];
        }
    }
    for(int i = 0; i < n; i++)      //Second-time Array Traversal
    {
       if(a[i] < secondMin && a[i] != min)
       {
          secondMin = a[i];
        }
    }
  return secondMin;               //Return the second smallest element
}
int secondLargest(int a[],int n)
{
    int max = a[0];
    int secondMax = a[1] ;
    for(int i = 0; i < n; i++)     //First-time Array Traversal
    {
       if(a[i] > max)
       {
          secondMax = max;
          max = a[i];
        }
    }
    for(int i = 0; i < n; i++)     //Second-time Array Traversal
    {
       if(a[i] > secondMax && a[i] != max)
       {
          secondMax = a[i];
        }
    }
  return secondMax;                //Return the second largest element
}
int main()
{
    int n;
    printf("Enter the number of elements:");
    scanf("%d",&n);
    printf("Enter the array elements :");
    int a[n];                      //Array Declaration
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    if(n<2)
    {
        printf("Invalid Input");
    }
    else
    {
       int sS=secondSmallest(a,n);
       printf("The second smallest element is %d",sS);
       printf("\n");
       int sL=secondLargest(a,n);
       printf("The second largest element is %d",sL);
    }
    return 0;
}


Enter the number of elements: 5
Enter the array elements : 98 67 45 52 90
The second smallest element is 52
The second largest element is 90

Program 3: To Find the Second Smallest and Second Largest Element in an Array

This is the most efficient way to find the second smallest and second largest element in an array. In a single traversal first, find the smallest and largest element, then skip it and find the next smallest and largest element in the array.

Algorithm

  1. Start
  2. Declare an array.
  3. Ask the user to initialize the array.
  4. Initialize the min, second_min, max, second_max as the first element of the array
  5. Start traversing the array and assign the smallest and largest element as min and max respectively. Now, in the same traversal skip the smallest and the largest element and find the next smallest and largest element.
  6. Print the second smallest and second largest element.
  7. Stop.

Below is the code for the same.

#include <stdio.h>

int secondSmallest(int arr[],int n)
{
    int min = arr[0];
    int second_min = arr[0] ;
    int i;
    for(i = 0; i < n; i++)       //Array Traversal
    {
       if(arr[i] < min)
       {
          second_min = min;
          min = arr[i];
       }
       else if(arr[i] < second_min && arr[i] != min)      //Check for second smallest
       {
          second_min = arr[i];
       }
    }
   return second_min;             //Return second smallest
}
int secondLargest(int arr[],int n)
{
    int i, first, second;
    if (n < 2) {
        printf(" Invalid Input ");
        return;
    }
    first = second =arr[0];        //Array Traversal
    for (i = 0; i < n; i++) 
    {
        if (arr[i] > first) 
        {
            second = first;
            first = arr[i];
        }
 
        else if (arr[i] > second && arr[i] != first)      //Check for second largest
        {
            second = arr[i];
        }
    }
    return second;                //Return second largest
}
int main() {
    int n;
    printf("Enter the number of elements:");
    scanf("%d",&n);
    printf("Enter the array elements :");
    int a[n];                    //Array Declaration
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    if(n<2)
    {
        printf("Invalid Input");
    }
    else
    {
        int sS=secondSmallest(a,n);
        printf("The second smallest element is %d",sS);
        printf("\n");
        int sL=secondLargest(a,n);
        printf("The second largest element is %d",sL);
    }
    return 0;
}


Enter the number of elements:5
Enter the array elements :67 87 93 45 32
The second smallest element is 45
The second largest element is 87



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight