How to find second largest number in an array in Java?

后端 未结 10 893
野的像风
野的像风 2021-01-01 05:57

I\'m just practicing some MIT java assignments. But, I\'m not sure how to find the second largest number. http://ocw.csail.mit.edu/f/13

  public class Marath         


        
相关标签:
10条回答
  • 2021-01-01 06:14

    Generally speaking:

    Have two values -- "largest" and "notQuite".

    Initialize both to -9999 or whatever.

    Scan through your list. If the number is larger than "largest", set "largest" to that number. But before you do that, copy the old "largest" value to "notQuite".

    If, on the other hand, the number is smaller than "largest" but is larger than "notQuite", set "notQuite" to that number.

    When you're done examining all the numbers, "notQuite" contains the second-largest.

    And note that, as you fill in the above numbers, you can also keep a "largestIndex" and "notQuiteIndex" and fill those in with the corresponding array index values, so you can identify the "winning" value. Unfortunately, though, if there are multiple identical "largest" or "secondLargest" values the simple index scheme doesn't work and you need to keep a list of some sort.

    0 讨论(0)
  • 2021-01-01 06:14
    public void findMax(int a[]) {
        int large = Integer.MIN_VALUE;
        int secondLarge = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            if (large < a[i]) {
                secondLarge = large;
                large = a[i];
            } else if (a[i] > secondLarge) {
                if (a[i] != large) {
                    secondLarge = a[i];
                }
            }
        }
        System.out.println("Large number " + large + " Second Large  number " + secondLarge);
    }
    

    The above code has been tested with integer arrays having duplicate entries, negative values. Largest number and second largest number are retrieved in one pass. This code only fails if array only contains multiple copy of same number like {8,8,8,8} or having only one number.

    0 讨论(0)
  • 2021-01-01 06:15
    int largest=time[0];
    int secondLargest=largest;
    for(int i=0;i<time.length;i++){
        if(time[i]>largest){
            secondLargest=largest;
            largest=time[i];
        }
        else if(secondLargest<time[i] && time[i]<largest || secondLargest>=largest)
            secondLargest=time[i];
    }
    return secondLargest;
    
    0 讨论(0)
  • 2021-01-01 06:19

    Find the second largest element in the given array:

    public static void findSecondMax(){
            int[] arr = {3, 2, 20, 4, 1, 9, 6, 3, 8};
    
            int max = 0;
            int secondMax = 0;
    
            for(int i =0; i< arr.length; i++) {
    
                if(max < arr[i]) max = arr[i];
    
                if((max > arr[i]) && (secondMax < arr[i])) secondMax = arr[i];
    
            }
    
            System.out.println(secondMax);
        }
    
    0 讨论(0)
  • 2021-01-01 06:20

    Instead of resorting to sorting the array, you can simply do the following:

    • Keep a largestValue and a secondLargestValue
    • Loop through the entire array once, for each element:
      • Check to see if the current element is greater than largestValue:
        • If so, assign largestValue to secondLargestValue, then assign the current element to largestValue (think of it as shifting everything down by 1)
        • If not, check to see if the current element is greater than secondLargestValue
          • If so, assign the current element to secondLargestValue
          • If not, do nothing.

    O(n) run time

    O(1) space requirement

    0 讨论(0)
  • 2021-01-01 06:22

    Sorting the array simply to find an order statistics is too wasteful. You can find the second largest element by following an algorithm that resembles the one that you already have, with an additional variable representing the second largest number.

    Currently, the next element could be larger than the max or equal to/smaller than the max, hence a single if is sufficient:

    if (times[i] > maxValue) {
        maxValue = times[i];
    }
    

    With two variables to consider, the next element could be

    • Greater than the max - the max becomes second largest, and the next element becomes the max
    • Smaller than the max but greater than the second largest - the next element becomes second largest.

    A special care must be taken about the initial state. Look at the first two items, and assign the larger one to the max and the smaller to the second largest; start looping at the element number three, if there is one.

    Here is how you can code it:

    if (times[i] > maxValue) {
        secondLargest = maxValue;
        maxValue = times[i];
    } else if (times[i] > secondLargest) {
        secondLargest = times[i];
    }
    
    0 讨论(0)
提交回复
热议问题