Find the third largest number in an array in Java

By | April 26, 2020

Find the third largest number in an array in Java:

In this article, learn how to find the third largest number in an array in java.

Answer: 40

Explanation: 

1. Sort the numbers in the array by comparing the adjacent numbers. Means if an array is having three numbers i.e 2,1,3, then 

compare 2 and 1: As 2 is greater than 1, switch the position of both number. Now the array becomes 1,2,3.

compare 2 and 3: As 2 is not greater than 3, no need to change the position of these two numbers.

So the final array becomes {1, 2, 3}

2. Now we can find the third largest number based on its index position in the array.

Find the third largest number in an array in java using Arrays:

Output: third largest number in array is: 30

Explanation:

1 Sort the number in array using Arrays.sort() method.

2 Find the position of number using its index position in the array.

 

Conclusion:

In this tutorial, you have learned how to find the third largest number in an array in Java in two ways. First one is by sorting the numbers using our logic and second one is with the help of Arrays.sort() method.

Recommended Posts:

Java program to find largest number in array

Convert String to String array in java

Covert String to int in Java

Star pattern programs in Java

Real-time selenium interview questions

 

Tags: java program to find third largest number in an array, find the third largest number in an array in java

Leave a Reply

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