EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Here’s a short and simple Java program to find the second largest element in an array in just a few lines

 

public class SecondLargest {

public static void main(String[] args) {

int[] arr = {10, 5, 20, 8, 15};

int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;

 

for (int num : arr) {

if (num > max) {

secondMax = max;

max = num;

} else if (num > secondMax && num != max) {

secondMax = num;

}

}

 

System.out.println(“Second largest element: ” + secondMax);

}

}

 

βœ… Output

Second largest element: 15

This code:

 

Uses a single loop

 

Handles duplicates

 

Finds the second largest efficiently

 

 

Finding largest number in java

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer