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
#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:
- We declare an integer array
arr[]
with elements. - We determine the size of the array using
sizeof(arr) / sizeof(arr[0])
. - Two variables,
first
(largest) andsecond
(second largest), are initialized toINT_MIN
. - We loop through the array:
- If an element is greater than
first
, we updatesecond
withfirst
and then updatefirst
. - If an element is greater than
second
but not equal tofirst
, we updatesecond
.
- If an element is greater than
- After the loop, we check if
second
was updated; if not, there is no second largest element.
References:
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
#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:
- We define a function
sortDescending()
that sorts the array in descending order using a nested loop. - Inside
sortDescending()
, we use a basic sorting algorithm (Bubble Sort) to arrange elements. - After sorting, the largest element is at index
0
, and the second largest is at index1
. - In the
main()
function, we callsortDescending()
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:
- Using a single pass: Maintains two variables to track the largest and second largest elements.
- 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.