C Program to Find Second Largest Element in Array

Here is the C program to find the second largest element in an unsorted array. Given an array of integers of length N, we have to find the second largest element in array without sorting the array.

For Example
Let inputArray is an integer array of seven elements.
int inputArray[7] = {2, -3, 0, 5, 9, 13, 1};
Second largest element of inputArray is 9

Algorithm to find second largest element in an array
Let inputArray is an array of length N, and maxElement and secondMaxElement are two integer variables to store maximum and second maximum element of array.
  • We need atleast two elements in array to find second largest element in array.

  • Initialize maxElement and secondMaxElement with INT_MIN. INT_MIN is the minimum value that can be represented by a signed int. INT_MIN macro is defined in limits.h header file.

  • Traverse inputArray from first element to last element.

  • if(current_element > maxElement) then secondMaxElement = maxElement; and maxElement = current_element; because if we found an element which is greater than current maximum element then current maximum element will become second maximum element.

  • Else If(current_element > secondMaxElement) then secondMaxElement = current_element; this mean current_element is greater than secondMaxElement but smaller than maxElement.

Time Complexity : O(n). This algorithm traverses inputArray only once.

C program to find second largest element in array without sorting

Below program first takes number of elements in array as input from user using scanf function. Number of elements in array must be greater than or equal to two, otherwise there won't be any second largest element in array. Then, it takes array elements as input from user using a for loop. Next we initialize maxElement and secondMaxElement by INT_MIN as explained above.

Then it traverses inputArray and compare each element with maxElement and secondMaxElement and update them as per the above mentioned algorithm.

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

int main(){
    int inputArray[500], elementCount, counter;
    int maxElement, secondMaxElement;
    
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    if(elementCount < 2){
        printf("Number of elements should be more \
            than or equal to two");
        return 0;
    }
        
    printf("Enter %d numbers \n", elementCount);
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    
    maxElement = secondMaxElement = INT_MIN;
    
    for(counter = 0; counter < elementCount; counter++){
        if(inputArray[counter] > maxElement){
            secondMaxElement = maxElement;
            maxElement = inputArray[counter];
        } else if (inputArray[counter] > secondMaxElement 
            && inputArray[counter] != maxElement){
            secondMaxElement = inputArray[counter];
        }
    }
    
    printf("Second Maximum element: %d", secondMaxElement);
        
    return 0;
}
Output
Enter number of elements in array: 6
Enter 6 numbers
5 -2 8 1 0 3
Second Maximum element: 5
Enter number of elements in array: 5
Enter 5 numbers
0 1 0 0 1
Second Maximum element: 0

To find second largest element in array, we can sort input array and then select second last element of sorted array. We can use any standard sorting algorithm to sort an integer array like quick sort, merge sort etc.

Related Topics
C Program to find minimum element in an array
C program to delete duplicate elements from an array
C program to delete duplicate elements from an array
C program to find sum of array elements using recursion
C program to delete duplicate elements from an array
C program to reverse an array using recursion
C program to find sum of digits of a number using recursion
C Program to print fibonacci series
List of all C Programs