Find the third largest no in Java

I am a little confused in this simple program. I need to find the third largest not in the array. I made some code, but I get only the second largest without problems in the 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); } } 
+4
source share
11 answers

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 is no longer needed.

Code Explanation:

We check that if the entered number is greater than the largest, move the third, second and first largest values ​​one level up. If the entered value is greater than the 2nd largest and smaller than the highest, move levels 3 and 2 up. If the entered values ​​are correlated than the 3rd largest and less than the 2nd largest, then move the 3rd largest to the entered value.

+12
source

Collection 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)); 
+4
source
  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]; } 
+2
source

Use java list, sort it. Take the third item.

java.util.Collections.sort ()

+1
source
 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]; } } 
+1
source

Use the Integer array, and then sort it with Collections and just select the element you want:

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); 

Output:

 [0, 1, 2, 3, 6] 

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

You can also cancel collection sorting. Check the documentation .

+1
source

If you want this 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 problem is that you are setting thirdLargest as secondLargest, which has already been defined as less than arr [i]. Then you check whether thirdLargest will be smaller than arr [i] (which is guaranteed, since it was set to the second largest in the external state), and then setting it to arr [i]. Try to remove

 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 the second but more than the thirdGreatest. (see Jens above), something like:

+1
source

Just loop through the entire array and track the three largest numbers.

Or you can sort it, and then return the third item on top.

0
source

Once you have included the array, just call Arrays.sort (array) in the array (so in your case Arrays.sort (arr);). This sorts it in ascending order, then you can just get 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]); 
0
source

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]); } 
0
source
 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); } } 
0
source

Source: https://habr.com/ru/post/1432457/


All Articles