Find the Second Largest Element in an Array in C

To find the second largest element in an array in C, we can iterate through the array while keeping track of the largest and second largest elements. This requires a single or double pass through the array, updating the values accordingly.


Examples to Find the Second Largest Element

1. Finding the Second Largest Element Using a Single Pass

In this example, we traverse the array once while maintaining two variables: one for the largest element and another for the second largest. The second largest is updated whenever a new maximum is found.

main.c

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

int main() {
    int arr[] = {12, 34, 45, 9, 8, 23};
    int size = sizeof(arr) / sizeof(arr[0]);

    int first = INT_MIN, second = INT_MIN;

    for (int i = 0; i < size; i++) {
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second && arr[i] != first) {
            second = arr[i];
        }
    }

    if (second == INT_MIN) {
        printf("No second largest element found.");
    } else {
        printf("Second largest element: %d", second);
    }

    return 0;
}

Explanation:

  1. We declare an integer array arr[] with elements.
  2. We determine the size of the array using sizeof(arr) / sizeof(arr[0]).
  3. Two variables, first (largest) and second (second largest), are initialized to INT_MIN.
  4. We loop through the array:
    • If an element is greater than first, we update second with first and then update first.
    • If an element is greater than second but not equal to first, we update second.
  5. After the loop, we check if second was updated; if not, there is no second largest element.

References:

  1. C For Loop
  2. C If-Else

Output:

Second largest element: 34

2. Finding the Second Largest Element Using Sorting

In this example, we first sort the array in descending order and then return the second element as the second largest.

main.c

</>
Copy
#include <stdio.h>

void sortDescending(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] < arr[j]) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {15, 2, 48, 36, 27};
    int size = sizeof(arr) / sizeof(arr[0]);

    sortDescending(arr, size);

    printf("Second largest element: %d", arr[1]);

    return 0;
}

Explanation:

  1. We define a function sortDescending() that sorts the array in descending order using a nested loop.
  2. Inside sortDescending(), we use a basic sorting algorithm (Bubble Sort) to arrange elements.
  3. After sorting, the largest element is at index 0, and the second largest is at index 1.
  4. In the main() function, we call sortDescending() and print the second largest element.

Output:

Second largest element: 36

Conclusion

We explored different ways to find the second largest element in an array:

  1. Using a single pass: Maintains two variables to track the largest and second largest elements.
  2. Using sorting: Sorts the array and selects the second element.

Both approaches work efficiently depending on the scenario. The first approach is more optimized for performance, while the second approach can be useful if sorting is required for other operations.