Java

How to Find the Second Largest Number in an Array in Java

In programming languages, arrays are the extensive collection of data consisting of the same data types. Moreover, you can also check and calculate each element position by inserting the offset into the root value. Sometimes we need to find the number inside the array without sorting it. To do so, various methods are used for this purpose, but the ideal approach is to calculate the random number by running the two loops consecutively.

This post will explain the method for finding the second largest number in an array in Java.

How to Find the Second Largest Number in an Array in Java?

To find and calculate the second largest number in an array in Java, first of all, sort the array and then print the second largest number. For practical implications, check out the below-stated example.

Example

First, initialize the variable along with data type:

int t, size;

Define an array and add the elements inside the array:

int arr[] = {1, 28, 38, 93, 46, 97};

Set the size of the array by specifying the array length:

size = arr.length;

Now, utilize the “for” loop to iterate the element that will compare the first two elements of the defined array:

  • If the first number of the array is greater than the second, then swap these numbers with each other.
  • After that, perform a comparison with the remaining element with the same procedure and repeat this until the end of the array:
for(int i = 0; i<size; i++ ){

for(int j = i+1; j<size; j++){

if(arr[i]>arr[j]){

   t = arr[i];

arr[i] = arr[j];

arr[j] = t;

  }

 }

}

After performing the comparison, print the second number on the console with the help of the “println()” method and pass the arguments for the second largest number:

System.out.println("2nd Largest Number: "+arr[size-2]);

As a result, the second largest number will be printed out on the console:

Let’s discuss another example for finding the second largest number. To do so, follow the below code snippet.

Define a number array and add the element inside the array:

int arr[] = {67, 89, 52, 63, 78, 75};

Next, indicate the size of the array by specifying the array length:

int size = arr.length;

Utilize the “sort()” method and pass the parameter to it. It will sort all the array elements in ascending order:

Arrays.sort(arr);

Then, print the array by invoking the “println()” method and pass the argument to store the element in a string with the help of the “toString()” method:

System.out.println("Array ::"+Arrays.toString(arr));

Now, declare a variable to store the second largest element and use “arr[size-2]”:

int result = arr[size-2];

Print the result on the console:

System.out.println("Second Largest ::"+result);

It can be noticed that the array has been sorted successfully and the second largest number will be displayed on the console:

That’s all about finding the second largest number in an array in Java.

Conclusion

To find/calculate the second largest number in an array in Java, first of all, sort the array and then print the second largest number. To do so, you can use the “iterative method” as well as “array.sort()” method for this purpose. This post has demonstrated the method for finding the second largest number in an array in Java.

About the author

Hafsa Javed