C Program to Find Second Largest Element in Array
if conditions; for example: if(arr[i] > largest) { second = largest; largest = arr[i]; } else if(arr[i] > second && arr[i] != largest) { second = arr[i]; }.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <limits.h> int main() { int arr[] = {5, 3, 9, 1, 6}; int n = sizeof(arr)/sizeof(arr[0]); int largest = INT_MIN, second = INT_MIN; for(int i = 0; i < n; i++) { if(arr[i] > largest) { second = largest; largest = arr[i]; } else if(arr[i] > second && arr[i] != largest) { second = arr[i]; } } if(second == INT_MIN) { printf("Second largest element does not exist\n"); } else { printf("Second largest element is %d\n", second); } return 0; }
Dry Run
Let's trace the array {5, 3, 9, 1, 6} through the code to find the second largest element.
Initialize variables
largest = INT_MIN, second = INT_MIN
Check element 5
5 > largest(INT_MIN), so second = INT_MIN, largest = 5
Check element 3
3 > second(INT_MIN) and 3 != largest(5), so second = 3
Check element 9
9 > largest(5), so second = largest(5), largest = 9
Check element 1
1 > second(5)? No, no change
Check element 6
6 > second(5) and 6 != largest(9), so second = 6
End of array
largest = 9, second = 6
| Iteration | Element | Largest | Second Largest |
|---|---|---|---|
| 1 | 5 | 5 | INT_MIN |
| 2 | 3 | 5 | 3 |
| 3 | 9 | 9 | 5 |
| 4 | 1 | 9 | 5 |
| 5 | 6 | 9 | 6 |
Why This Works
Step 1: Initialize largest and second largest
Start with smallest possible values so any array element will be larger and update these variables.
Step 2: Update largest and second largest
When a bigger element than largest is found, shift largest to second largest and update largest.
Step 3: Handle duplicates and smaller elements
Only update second largest if element is less than largest but greater than current second largest.
Alternative Approaches
#include <stdio.h> #include <stdlib.h> #include <limits.h> int compare(const void *a, const void *b) { return (*(int*)a - *(int*)b); } int main() { int arr[] = {5, 3, 9, 1, 6}; int n = sizeof(arr)/sizeof(arr[0]); qsort(arr, n, sizeof(int), compare); int largest = arr[n-1]; int second = INT_MIN; for(int i = n-2; i >= 0; i--) { if(arr[i] != largest) { second = arr[i]; break; } } if(second == INT_MIN) { printf("Second largest element does not exist\n"); } else { printf("Second largest element is %d\n", second); } return 0; }
#include <stdio.h> #include <limits.h> int main() { int arr[] = {5, 3, 9, 1, 6}; int n = sizeof(arr)/sizeof(arr[0]); int largest = INT_MIN, second = INT_MIN; for(int i = 0; i < n; i++) { if(arr[i] > largest) largest = arr[i]; } for(int i = 0; i < n; i++) { if(arr[i] > second && arr[i] < largest) second = arr[i]; } if(second == INT_MIN) { printf("Second largest element does not exist\n"); } else { printf("Second largest element is %d\n", second); } return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program scans the array once, so time complexity is O(n), where n is the number of elements.
Space Complexity
Only a few variables are used, so space complexity is O(1), constant extra space.
Which Approach is Fastest?
The single pass method is fastest and most efficient compared to sorting or two-pass methods.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single pass scan | O(n) | O(1) | Fastest, minimal memory |
| Sorting then pick | O(n log n) | O(1) | Simple code, slower for large arrays |
| Two passes | O(n) | O(1) | Clear logic, slightly less efficient |