'Find third largest no in Java

I'm little confused with this simple program.I have to find third largest no in array.I have done some code but getting only second largest no problem in third largest no so please suggest me what is wrong with this solution:

class ArrayExample {
    public static void main(String[] args) {
        int secondlargest = Integer.MIN_VALUE;
        int thirdlargest = Integer.MIN_VALUE;
        int largest = Integer.MIN_VALUE;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter array values: ");
        int arr[] = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
            if (largest < arr[i]) {
                secondlargest = largest;
                largest = arr[i];
            }
            if (secondlargest < arr[i] && largest != arr[i]) {
                thirdlargest = secondlargest;
                secondlargest = arr[i];
                if (thirdlargest < arr[i] && secondlargest != arr[i])
                    thirdlargest = arr[i];
            }

        }
        System.out.println("Second Largest number is: " + secondlargest
                + "\nThird largest number is=====" + thirdlargest);
    }
}


Solution 1:[1]

I would try something like this:

if (largest < ar[i]) {
    thirdlargest = secondlargest;
    secondlargest = largest;
    largest = arr[i];
} else if (secondlargest < ar[i]) {
    thirdlargest = secondlargest;
    secondlargest = ar[i];
} else if (thirdlargest < ar[i]) {
    thirdlargest = ar[i];
}

Not tested but I think the second IF isn't needed anymore.

Code Explanation:

We are verifying that if an entered number is greater than largest then move the third, second and 1st largest values one level up. If an entered value is greater than 2nd largest and less than largest, then move 3 and 2 one level up. If entered values is greated than 3rd largest and less than 2nd largest then move 3rd largest to the entered value.

Solution 2:[2]

Collections API. Here is an example:

    List list = Arrays.asList(new Integer[] {1, 2, 29, 4, 28, 6, 27, 8});
    Collections.sort(list);
    System.out.print(list.get(list.size()-3));

Solution 3:[3]

        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];
        }

Solution 4:[4]

Use a java list, sort it. Take the third element.

java.util.Collections.sort()

Solution 5:[5]

for (int i = 0; i < arr.length; i++)
 {
        arr[i] = input.nextInt();
        if (largest < arr[i]) {
            secondlargest = largest;
            largest = arr[i];
            continue;
        }
        if (secondlargest <= arr[i] && largest > arr[i])
        {
            thirdlargest = secondlargest;
            secondlargest = arr[i];
            continue;
        }
        if (thirdlargest <= arr[i] && secondlargest > arr[i])
        {
                thirdlargest = arr[i];
        }

 }

Solution 6:[6]

Use Integer array and then sort it using Collections and just pick the element you need:

Code:

System.out.println("Enter array values: ");
Integer arr[] = new Integer[5];
for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
}
List<Integer> list = Arrays.asList(arr);
Collections.sort(list);
System.out.println(list);

The output is:

[0, 1, 2, 3, 6]

So, now select the 3rd larget number as list.get(list.size()-3)).

You can also reverse sort the Collection. Check it's documentation.

Solution 7:[7]

If you want that code to work, I think the problem is here:

 if (secondlargest < arr[i] && largest != arr[i]) {
                thirdlargest = secondlargest;
                secondlargest = arr[i];
                if (thirdlargest < arr[i] && secondlargest != arr[i])
                    thirdlargest = arr[i];
            }

The issue is you are setting thirdLargest to be secondLargest, which has already been identified as less than arr[i]. You are then testing if thirdLargest is less than arr[i] (which it is guaranteed to be as it it has been set to second largest within the outer condition) and then setting it to arr[i]. Try removing the

if (thirdlargest < arr[i] && secondlargest != arr[i])
                        thirdlargest = arr[i];

and if that doesn't work try adding a third separate condition to cover cases where arr[i] is less than secondGreatest but greater then thirdGreatest. (see Jens answer above), something like :

Solution 8:[8]

Just cycle through the whole array and keep track of the three largest numbers.

Or you could sort it and then return the third element from the top.

Solution 9:[9]

Once you have the array read in, just call Arrays.sort(array) on the array (so in your case Arrays.sort(arr); ). This will sort it into ascending order, then you can just retrieve the element arr.length-2

Full description here: http://www.homeandlearn.co.uk/java/sorting_arrays.html

Example:

    Scanner input = new Scanner(System.in);
            System.out.println("Enter array values: ");
            int arr[] = new int[5];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = input.nextInt();
        }
    Arrays.sort(arr);
System.out.println("Second Largest number is: " + arr[4]
                + "\nThird largest number is=====" + arr[3]);

Solution 10:[10]

Try this code,

public static void main(String[] args) {

    int arr[] = {67, 56, 87, 42};

    for (int i = 0; i <arr.length - 1; i++) {
        if (arr[i] < arr[i + 1]) {
            int swap = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = swap;       
        }
    }

    System.out.println("third highest element is: " + arr[2]);
}   

Solution 11:[11]

package algo;

public class LargestNumbers {

    public static void main(String args[]){
        int arr[] = new int[]{5,2,3,4,6};

        int largest=Integer.MIN_VALUE;;
        int secondLargest=Integer.MIN_VALUE;;
        int thirdLargest=Integer.MIN_VALUE;;


        for(int i=0;i<arr.length;i++){

              if(largest<arr[i])
                {
                    thirdLargest=secondLargest;
                    secondLargest=largest;
                    largest = arr[i];
                }

               else if((secondLargest<arr[i])&&(arr[i]!=largest))
                {
                    thirdLargest=secondLargest;
                    secondLargest = arr[i];
                }

               else if((thirdLargest<arr[i])&&(arr[i]!=secondLargest))
                {
                    thirdLargest = arr[i];
                }

        }//for


         System.out.println("Numbers are: " + largest + " "  + secondLargest
                    + "\nThird largest number is=====" + thirdLargest);
    }

}

Solution 12:[12]

package arrays;

import java.util.Arrays;
public class ArraythirdHighestvalues {
    //method 1 
    public static  int highest(int []arr,int length)
    {
        int temp;
        for(int i=0;i<length;i++)
        {
            for(int j=i+1;j<length;j++)
            {
                if(arr[i]>arr[j])
                {
                    temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        return arr[length-2];
    }
    public static void main(String[] args) {
        int arr[]= {1,3,5,6,7,2,};
        System.out.println(highest(arr,arr.length));
        System.out.println("method 2 to find array second higest element value --");
        Arrays.parallelSort(arr);
        System.out.println(arr[arr.length-3]);
        
        
        
    }
    
}

Solution 13:[13]

package algo;

public class LargestNumbers {

public static void main(String args[]){
    int arr[] = new int[]{5,2,3,4,6};

    int largest=Integer.MIN_VALUE;;
    int secondLargest=Integer.MIN_VALUE;;
    int thirdLargest=Integer.MIN_VALUE;;


    for(int i=0;i<arr.length;i++){

          if(largest<arr[i])
            {
                thirdLargest=secondLargest;
                secondLargest=largest;
                largest = arr[i];
            }

           else if((secondLargest<arr[i])&&(arr[i]!=largest))
            {
                thirdLargest=secondLargest;
                secondLargest = arr[i];
            }

           else if((thirdLargest<arr[i])&&(arr[i]!=secondLargest))
            {
                thirdLargest = arr[i];
            }

    }//for


     System.out.println("Numbers a`enter code here`re: " + largest + " "  + secondLargest
                + "\nThird largest number is=====" + thirdLargest);
}

}

Solution 14:[14]

Stream.of(23, 11, 96, 33, 89, 63, 17, 44 ,67, 55, 7, 76,42).sorted(Comparator.reverseOrder()).distinct().limit(3).skip(2).findFirst().get();

Initialize Arrays.asList(new int[]{23, 11, 9}).stream()