Skip to main content

Command Palette

Search for a command to run...

Second largest element

Updated
3 min readView as Markdown

Problem Statement:

Given an array of integers, find the second largest element in the array.

Steps to Solve the Problem:

  1. Initialization:

    • Initialize two variables: firstLargest and secondLargest to the smallest possible integer value (Integer.MIN_VALUE).
  2. Iterate Through the Array:

    • For each element in the array, compare it with firstLargest and secondLargest and update these variables accordingly:

      • If the current element is greater than firstLargest, update secondLargest to firstLargest and then update firstLargest to the current element.

      • If the current element is greater than secondLargest but not equal to firstLargest, update secondLargest to the current element.

  3. Return the Result:

    • After the loop, check if secondLargest is still Integer.MIN_VALUE. If so, return null, meaning there is no second largest element. Otherwise, return secondLargest.

Dry Run:

Let's perform a dry run with the array [12, 35, 1, 10, 34, 1].

Initial State:

  • firstLargest = Integer.MIN_VALUE (-2147483648)

  • secondLargest = Integer.MIN_VALUE (-2147483648)

Array Iteration:

  1. First Element: 12

    • Compare 12 with firstLargest (-2147483648):

      • 12 > firstLargest, so update secondLargest to firstLargest (-2147483648) and firstLargest to 12.
    • State:

      • firstLargest = 12

      • secondLargest = -2147483648

  2. Second Element: 35

    • Compare 35 with firstLargest (12):

      • 35 > firstLargest, so update secondLargest to firstLargest (12) and firstLargest to 35.
    • State:

      • firstLargest = 35

      • secondLargest = 12

  3. Third Element: 1

    • Compare 1 with firstLargest (35):

      • 1 is not greater than firstLargest, so check if it's greater than secondLargest (12):

      • 1 is not greater than secondLargest, so no changes.

    • State:

      • firstLargest = 35

      • secondLargest = 12

  4. Fourth Element: 10

    • Compare 10 with firstLargest (35):

      • 10 is not greater than firstLargest, so check if it's greater than secondLargest (12):

      • 10 is not greater than secondLargest, so no changes.

    • State:

      • firstLargest = 35

      • secondLargest = 12

  5. Fifth Element: 34

    • Compare 34 with firstLargest (35):

      • 34 is not greater than firstLargest, so check if it's greater than secondLargest (12):

      • 34 > secondLargest, so update secondLargest to 34.

    • State:

      • firstLargest = 35

      • secondLargest = 34

  6. Sixth Element: 1

    • Compare 1 with firstLargest (35):

      • 1 is not greater than firstLargest, so check if it's greater than secondLargest (34):

      • 1 is not greater than secondLargest, so no changes.

    • Final State:

      • firstLargest = 35

      • secondLargest = 34

Final Result:

  • The second largest element in the array is 34.

Java Code Recap:

javaCopy codepublic class SecondLargestElement {
    public static Integer findSecondLargest(int[] arr) {
        if (arr.length < 2) {
            return null; // Not enough elements
        }

        int firstLargest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > firstLargest) {
                secondLargest = firstLargest;
                firstLargest = arr[i];
            } else if (arr[i] > secondLargest && arr[i] != firstLargest) {
                secondLargest = arr[i];
            }
        }

        return secondLargest == Integer.MIN_VALUE ? null : secondLargest;
    }

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

        if (result != null) {
            System.out.println("The second largest element is: " + result);
        } else {
            System.out.println("There is no second largest element.");
        }
    }
}

This dry run shows how the algorithm processes each element in the array and correctly identifies the second largest element.

More from this blog

Amit singh's blog

235 posts