java

Find Second Largest Element()

Parameters: int arr[], int arr_size

an integer array and the size of that array

Returns: integer that represents the second largest number in array

This Java function sifts through an array of integers to determine the second largest element. Useful for gaining an understanding of sorting and comparison operations in Java.

variables
conditional statements
loops
array
comparison operations
Medium dificulty

Writing a Java Function to Find the Second Largest Element

Hello, programmer! We are glad you've joined us here. In the blog post we are going to show you how creating a function to find the second largest element in an array in Java can be interesting. We believe simplicity is key, and we promise to take you through each step without complicated jargon. Let's delve into it together!

Step-1: Initializing Variables

The first step involves declaring and initializing two variables; max and secondMax. We'll set them initially to Integer.MIN_VALUE which represents the minimum value an int can have in Java. We are doing this to cover the case when the array has both positive and negative elements.

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

Step-2: Iterate Over the Array

We proceed to a loop that would iterate over the provided array to find the maximum and second maximum values. Initially, max and secondMax are set to the smallest integer possible to guarantee they will be replaced by elements in the array.

Step-3: Updating max and secondMax

In each iteration, we check whether the current element array[i] is greater than max. If it indeed is, we update max and secondMax. secondMax gets the old value of max, and max is updated with the new highest value, array[i].

for(int i = 0; i < array.length; i++) {
    if(array[i] > max) {
        secondMax = max;
        max = array[i];
    }
    else if (array[i] > secondMax && array[i] < max) {
        secondMax = array[i];
    }
}

Step-4: Return the Second Largest

Once the loop is done, secondMax should contain the second largest element in the array. If the array only had one unique value, secondMax will be Integer.MIN_VALUE. We then return secondMax.

return secondMax;

Step-5: Complete Function

We combine all the steps above to form the final function findSecondLargest() that takes in an array of integers and returns the second largest integer.

public int findSecondLargest(int[] array) {
    int max = Integer.MIN_VALUE;
    int secondMax = Integer.MIN_VALUE;
    for(int i = 0; i < array.length; i++) {
        if(array[i] > max) {
            secondMax = max;
            max = array[i];
        }
        else if (array[i] > secondMax && array[i] < max) {
            secondMax = array[i];
        }
    }
    return secondMax;
}

Learn function in:

Find Second Largest Element

isolates the second-largest numerical value in an array

Learn more

Mathematical principle

The function operates by iterating over the array while maintaining two variables to track the largest and second largest values. If any number is greater than the current largest, the largest number is moved to the `second_largest` and that number becomes the `largest`. If it's between `largest` and `second_largest`, it becomes the `second_largest`. This paradigm exemplifies the principle of simple comparisons used in mathematical sorting and maximal/minimal operations.

Learn more