Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

Java Program to Find the Second Largest Element of an Array

Posted on July 15, 2025July 15, 2025 By Admin No Comments on Java Program to Find the Second Largest Element of an Array

Finding the second largest element in an array is a common coding challenge that builds on array traversal and conditional logic. It’s frequently asked in interviews and assessments to evaluate understanding of control flow, edge case handling, and array manipulation.

Given an array of integers and the task is to find the second largest element of the array, from basic loops to modern Java features like streams.

Considerations Before Coding

  1. Duplicate elements – What if the largest number occurs more than once?
  2. All elements are the same – Should we return a specific value or error?
  3. Array with fewer than 2 elements – Should we return a message or an exception?

Approach 1: Brute Force – Sort and Pick Second Last

Sort the array and pick the second last unique element.

// Java Program to Find the Second Largest 
// Element of an Array

import java.util.Arrays;

public class SecondLargestElement {
    public static void main(String[] args) {
        int[] arr = {12, 35, 1, 10, 34, 1};

        // Sort in ascending order
        Arrays.sort(arr);

        int n = arr.length;
        int largest = arr[n - 1];

        for (int i = n - 2; i >= 0; i--) {
            if (arr[i] != largest) {
                System.out.println("Second Largest Element: " + arr[i]);
                return;
            }
        }

        System.out.println("No Second Largest Element Found.");
    }
}

Time complexity: O(n log n) due to sorting.

Approach 2: Single Pass Using Two Variables

Maintain two variables – first (max) and second (second max) during a single array traversal.

// Java Program to Find the Second Largest 
// Element of an Array

public class SecondLargestElement {
    public static void main(String[] args) {
        int[] arr = {12, 35, 1, 10, 34, 1};

        if (arr.length < 2) {
            System.out.println("Array must contain at least two elements.");
            return;
        }

        int first = Integer.MIN_VALUE;
        int second = Integer.MIN_VALUE;

        for (int num : arr) {
            if (num > first) {
                second = first;
                first = num;
            } else if (num > second && num != first) {
                second = num;
            }
        }

        if (second == Integer.MIN_VALUE) {
            System.out.println("No second largest element found.");
        } else {
            System.out.println("Second largest element: " + second);
        }
    }
}

Time complexity: O(n)

Approach 3: Using Java Streams (Java 8+)

Use distinct(), sorted(), and limit() methods to retrieve the second largest.

import java.util.Arrays;

public class SecondLargestElement {
    public static void main(String[] args) {
        int[] arr = {12, 35, 1, 10, 34, 1};

        Integer[] result = Arrays.stream(arr)
            .distinct()
            .boxed()
            .sorted((a, b) -> b - a) // Sort in descending order
            .limit(2)
            .toArray(Integer[]::new);

        if (result.length < 2) {
            System.out.println("No second largest element found.");
        } else {
            System.out.println("Second largest element: " + result[1]);
        }
    }
}

Concise and expressive. Works well with modern Java (8+).

Asked in an Interview: Adobe

Java, Programs Tags:Java-Array, Java-Programs

Post navigation

Previous Post: Adobe Interview Experience | Senior QA Engineer | Automation Test for Noida Office [Latest 2025]
Next Post: Java Program to Check Whether a Given String is Palindrome or Not

Leave a Reply Cancel reply

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

Archives

  • July 2025
  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Selenium – A Web Application Testing Framework
  • Java Program to Check Whether a Given String is Palindrome or Not
  • Java Program to Find the Second Largest Element of an Array
  • Adobe Interview Experience | Senior QA Engineer | Automation Test for Noida Office [Latest 2025]
  • Java Program to Find the Largest Element of an Array

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme