Finding third largest element in an array using Java

Hi everyone, in this blog post, I am writing a Java program to find the third largest element in an array. When we are asked to write a program about finding the third largest element in an array, the trivial solution which comes to our mind is sorting the array and returning the third element from the end. But sorting an array is of time complexity O(nlogn) and a program always requires to minimize the total time complexity and space complexity. We can do the above program in time O(n).

The logic of the program is as follows:
  • Initialize three variables as firstLargest, secondLargest and thirdLargest to Integer.MIN_VALUE
  • Iterate the loop of elements in the array.
  • For each number, check the following steps
  • If firstLargest is less than the number, assign thirdLargest to secondLargest, secondLargest to firstLargest and firstLargest to the number.
  • Else if secondLargest is less than the number and if number not equal to firstLargest, then assign thirdLargest to secondLargest and secondLargest to the number.
  • Else if thirdLargest is less than the number and if number not equal to secondLargest, then assign thirdLargest to secondLargest.
  • After the loop is iterated the final value of thirdLargest corresponds tot he third largest element in an array.
The java program is as follows:

package com.blog.techjourney;

public class ThirdLargestElement {

    public static void main(String args[])
    {
    int arr[] = {100,6,10,7,11,12,3,2,100,17,8,9,100,91,981,5,63,94};
    int thirdLargestValue = findThirdLargestElement(arr);
    System.out.println(" The third largest element in the array is - "+thirdLargestValue);
    }
    
    public static int findThirdLargestElement(int array[])
    {
        if(array==null)
        {
            throw new ArithmeticException(" The array is empty");
        }
        
        else
        {
        int length = array.length;
        int firstLargest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;
        int thirdLargest = Integer.MIN_VALUE;
        
        for(int num=0; num<length;num++)
        {
            if(firstLargest<array[num])            {
                thirdLargest=secondLargest;
                secondLargest=firstLargest;
                firstLargest = array[num];
            }
            
            else if((secondLargest<array[num])&&(array[num]!=firstLargest))            

            {
                thirdLargest=secondLargest;
                secondLargest = array[num];
            }
            
            else if((thirdLargest<array[num])&&(array[num]!=secondLargest))            

            {
                thirdLargest = array[num];
            }
        }
        return thirdLargest;
        }
    }
}



The output of the program is as follows:

 The third largest element in the array is : 94


I hope this blog post gives everyone a clear idea about how to find the third largest element in an array. Generally this logic can be used for finding the nth largest element in an array.

Please feel free to comment below. Any suggestions will be appreciated.

Thank you very much for reading the blog.

Comments

  1. if i added largest element in two or more times in array above code fails, it gives incorrect result.

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++