Find the Second Largest Number in an Array in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include<stdio.h>

int main() {
    int n, a[100], max=-1e9, second=-1e9;
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d", &a[i]);
        if(a[i]>max) second=max, max=a[i];
        else if(a[i]>second && a[i]<max) second=a[i];
    }
    printf("%d", second);
}

C Output

Input:  
5  
3 7 1 9 4  

Output:  
7


C++ Program

#include<iostream>
using namespace std;

int main() {
    int n, x, max=-1e9, second=-1e9;
    cin >> n;
    while(n--) {
        cin >> x;
        if(x > max) second = max, max = x;
        else if(x > second && x < max) second = x;
    }
    cout << second;
}

C++ Output

Input:  
6  
10 20 5 6 20 7  

Output:  
10


JAVA Program

import java.util.*;

class S {
    public static void main(String[] a) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), max = Integer.MIN_VALUE, sec = Integer.MIN_VALUE;
        for(int i=0; i<n; i++) {
            int x = sc.nextInt();
            if(x > max) { sec = max; max = x; }
            else if(x > sec && x < max) sec = x;
        }
        System.out.print(sec);
    }
}

JAVA Output

Input:  
4  
9 2 5 9  

Output:  
5


Python Program

a = list(map(int, input().split()))
m = max(a)
print(max(x for x in a if x != m))

Python Output

Input:  
1 2 3 4 5  

Output:  
4


In-Depth Learning – Entire Concept in Paragraphs
What Does "Second Largest" Mean?
The second largest value in an array is the one that is less than the maximum, yet more than any other. This exercise teaches you comparisons, conditional logic, and edge case handling. It is frequently a sequel to finding the maximum and teaches you how to keep multiple values in track dynamically.

How the Code Works
The logic is as follows:

Set max and second to very small values.

Go through every element:

If current is greater than max → second = max, and max = current.

Else if current is greater than second but not equal to max → second.

This way we always keep the top two unique values.

In Python, we do it in a nice trick:

First find max(a) → the largest.

Then use a generator to get the max of the rest (x for x in a if x != m).

This method is quick, neat, and gets rid of duplicates right.

Example
Input:
[4, 8, 1, 3, 8, 7]

First max: 8

Ignore 8s, the rest: [4, 1, 3, 7]

Max of that = 7

Output:

7

Real-Life Analogy
Think of a race where runners finish in varying times. The fastest one wins first place. But who is second?

To find the second-largest number is similar to finding the silver medalist — the second-best performer after the champion. This is a well-utilized concept in competitions, ranking schemes, exam scores analysis, etc.

Where and When Is It Used?
This question is very helpful in:

Coding interview questions (TCS, Infosys, Amazon, etc.)

Rank-based algorithms

Identification of runner-ups in contests

Data analysis

Design of a leaderboard in games or apps

Usual go-to for more advanced problems such as k-th largest, sorting optimizations, or heap-based reasoning.

Time and Space Complexity
Operation\tComplexity
Time\tO(n) – just a single pass required
Space\tO(1) – only two additional variables used

In Python's approach using generator and list operations, it can use slightly higher memory, but still efficient for the majority of cases.

Python-Specific Tip
To prevent duplicates or guarantee unique results, you can make the list into a set:

python

a = list(map(int, input().split()))
print(sorted(set(a))[-2])
It results in the second largest unique number but needs to sort (O(n log n)).

SEO-Optimized Natural Paragraph for Ranking
Want to determine the second highest number in an array in C, C++, Java, or Python? This tutorial provides the neatest and fastest code examples to identify the second highest value in a list. Whether you are getting ready for technical interviews, coding contests, or competitive programming learning, knowing how to maintain top two values dynamically is important. This code employs conditional logic to refresh maximum and second maximum values within one loop without sorting. Master how to do this efficiently in all languages with real-world examples, code, and descriptions.