C Program to Find Second Largest Element in an Array

Here we’ll see the possible solutions of finding the second largest element in an array.

By Sorting the Array:

Easy solution that I can think of to find the second largest element in an array is to first sort the array and then pick the second or second last element of the array depending on whether the array is sorted in descending or ascending order.
In the following program we’ll first sort the array in ascending order using bubble sort. Once the array is sorted, then the second last element will be the second largest element.

#include <stdio.h>
#include <stdlib.h>

void bubble_sort(int *arr, int n)
{
  int i = 0, j = 0;
  int tmp = 0;

  for(i = 0; i < n-1; i++)
  {
    for (j = 0; j < n-i-1; j++)
    {
      if(arr[j+1] < arr[j])
      {
         tmp = arr[j];
         arr[j] = arr[j+1];
         arr[j+1] = tmp;
      }
    }
  }
}

int main()
{
  int n, i;
  int *arr = NULL;
  printf("Enter number of elements: ");
  scanf("%d", &n);

  arr = (int *)malloc(n*sizeof(int));
  if (arr == NULL)
  {
    printf("Memory allocation failed.\n");
    return 0;
  }

  for (i = 0; i < n; i++)
  {
    printf("Enter %d-th element: ", i+1);
    scanf("%d", arr+i);
  }
  printf("Input array: ");
  for (i = 0; i < n; i++)
  {
    printf(" %d", arr[i]);
  }
  printf("\n");

  /*Doing bubble sort*/
  bubble_sort(arr, n);

  printf("Second largest element of the array: %d\n", arr[n-2]); /*(n-1)-th element is the last one, (n-2)-th element is the second last.*/
  return 1;
}

Output of the Program:

Enter number of elements: 6
Enter 1-th element: 32
Enter 2-th element: 1
Enter 3-th element: 34
Enter 4-th element: 24
Enter 5-th element: 5
Enter 6-th element: 76
Input array:  32 1 34 24 5 76
Second largest element of the array: 34

Though this is very simple solution, it has some drawback. It time complexity is O(n2) whereas O(n) solution is possible. O(n2) is very inefficient compared to O(n).

Without Sorting

The following solution is by traversing the array once.

#include <stdio.h>
#include <stdlib.h>


int main()
{
  int n, i;
  int largest, second_largest;
  int *arr = NULL;
  printf("Enter number of elements: ");
  scanf("%d", &n);

  while(n < 2) {
    printf("You should have at least 2 elements\n");
    printf("Enter number of elements: ");
    scanf("%d", &n);
  }

  arr = (int *)malloc(n*sizeof(int));
  if (arr == NULL)
  {
    printf("Memory allocation failed.\n");
    return 0;
  }

  for (i = 0; i < n; i++)
  {
    printf("Enter %d-th element: ", i+1);
    scanf("%d", arr+i);
  }
  printf("Input array: ");
  for (i = 0; i < n; i++)
  {
    printf(" %d", arr[i]);
  }
  printf("\n");

  if(arr[0] > arr[1]) {
    largest = arr[0];
    second_largest = arr[1];
  } else {
    second_largest = arr[0];
    largest = arr[1];
  }

  for (i = 2; i < n; i++) {
    if(arr[i] > largest)
    {
      second_largest = largest;
      largest = arr[i];
    }
    else if (arr[i] > second_largest)
    {
      second_largest = arr[i];
    }
  }

  printf("Second largest element of the array: %d\n", second_largest);
  return 1;
}

In every step of the two largest elements are kept. The variables are initialized with the first element of the array assuming that it will be the desired result. From next iteration, the current element of the array is compared with the highest element. If the current element is highest element, the previous highest element will replace the second highest element and will be replaced by the current element. If the current element of the array is not greater than the highest element but greater than the second highest element, then it will replace the second highest element. At the end of the iteration, the variable second_largest will be the second highest element.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

2 thoughts on “C Program to Find Second Largest Element in an Array”

  1. Dear Publisher,
    Check Your non-iterative solution once for the decreasingly sorted elements.
    it misses certain necessary comparison.
    i.e. 5,4,3,2,1.

    1. Thank you very much for pointing that out. I resolved that problem. Please check if you see any other problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

2
1
0
0
0
0