💥Find Second Largest(Maximum) in Array ☕Java Interview Programs👨‍🎓Campus Interview Programs 🎁👩‍🏫Java

Опубликовано: 15 Март 2026
на канале: Guhan Tech Zone
6,986
100

🎯 Java Program – Find Second Maximum Element in Array

Welcome to our channel 🙏

In this video, we learn how to find the Second Largest (Second Maximum) element in an Array using Java.

This is a very popular Java interview program and helps beginners clearly understand:

✅ Array traversal
✅ Comparison logic
✅ Using two variables (first and second)
✅ Finding second largest in a single loop

This program is very useful for:

✔ Freshers
✔ College students
✔ Java beginners
✔ Campus placements

Please watch till the end and practice on your own laptop.

🧠 What is Second Maximum?

Second Maximum means:

👉 The second biggest number in the array.

Example:

Array:

2, 1, 4, 3, 9, 7, 8, 0, 10

Largest = 10
Second Largest = 9

🧠 Variables Used

arr → array elements
first → stores largest value
second → stores second largest value
i → loop counter

✅ Logic Explanation Step by Step
Step 1 – Declare array
int[] arr = {2,1,4,3,9,7,8,0,10};

Step 2 – Initialize variables
int first = 0;
int second = 0;

first holds maximum
second holds second maximum

Step 3 – Loop through array
for(int i=0; i is less than arr.length; i++)

Loop checks every element.

Step 4 – Compare with first (largest)
if(arr[i] is greater than first)

Then:

second = first;
first = arr[i];

Meaning:

Old largest moves to second
New value becomes largest

Step 5 – Compare with second
else if(arr[i] is greater than second)

Then:

second = arr[i];

This handles values between first and second.

🧪 Dry Run

Array:

2, 1, 4, 3, 9, 7, 8, 0, 10

Start:

first = 0
second = 0

Process:

2 → first becomes 2
1 → second becomes 1
4 → second becomes 2, first becomes 4
3 → second becomes 3
9 → second becomes 4, first becomes 9
7 → second becomes 7
8 → second becomes 8
0 → no change
10 → second becomes 9, first becomes 10

Final:

first = 10
second = 9

Step 6 – Print result
System.out.println(second);

Output:

9

⭐ Why Interviewers Ask This?

Tests:

✔ Array traversal
✔ Nested comparison
✔ Logical thinking
✔ Optimized single loop

⚠ Common Mistakes

❌ Forgetting to update second before first
❌ Using sorting instead of logic
❌ Initializing wrongly
❌ Missing else condition

second largest in array java
find second maximum java
java array interview programs
second highest number java
java logical programs

#Java
#ArrayPrograms
#SecondLargest
#JavaInterview
#ProgrammingTamil
#Freshers
#CampusPlacement