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

后端 未结 10 894
野的像风
野的像风 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:22
    private static int secLargest(int[] numbers) {
            int maxVal = 0;
            int nextMaxVal = 0;
            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] > maxVal) {
                    nextMaxVal = maxVal;
                    maxVal = numbers[i];
    
                }
                if (numbers[i] < maxVal) {
                    nextMaxVal = maxVal;
                    maxVal = numbers[i];
    
                }
            }
            return nextMaxVal;
    
        }
    
    0 讨论(0)
  • 2021-01-01 06:23

    It will also extract second largest number if largest number occours two times as well as in a single for loop.

    import java.util.*;
    public class SecondLargestInArray
    {
        public static void main(String[] args)
        {
            int arr[] = {99,14,46,47,86,92,52,48,36,66,85,92};
            int largest = arr[0];
            int secondLargest = arr[0];
            System.out.println("The given array is:" );
            for (int i = 0; i < arr.length; i++)
            {
                System.out.print(arr[i]+"\t");
            }
    
            for (int i = 0; i < arr.length; i++)
            {
                if (arr[i] > largest)
                {
                    secondLargest = largest;
                    largest = arr[i];
                }
                else if((arr[i]<largest && arr[i]>secondLargest) || largest==secondLargest)
                {
                    secondLargest=arr[i];
                }
            }
            System.out.println("\nLargest number is:" + largest);
            System.out.println("\nSecond largest number is:" + secondLargest);
        }
    }
    
    0 讨论(0)
  • 2021-01-01 06:24
        private void secondLargest(int arr[]){
    
        int maxOne=arr[0];
        int maxTwo=arr[1];
    
        for(int i=0;i<arr.length;i++){
    
            if(arr[i]>maxOne){
    
                maxTwo=maxOne;
                maxOne=arr[i];
            }else if (arr[i]>maxTwo) {
    
                maxTwo=arr[i];
            }
    
        }
    
        System.out.println(maxOne);
        System.out.println(maxTwo);
    }
    
    0 讨论(0)
  • 2021-01-01 06:24

    PHP ALGO

    if $arr is given array

    $a = 0; $b = 0; // These are two variables and set their value to minimum
     $i = 0 ; // this is incremental variable
     loop $i till count($arr)   // loop the array using foreach or for-loop till length of array
        if( $arr[i] > $a || $arr[i] > $b) 
           ($a < $b) ? $a = $arr[i]: $b = $arr[i] ; // this is conditional operator 
        loop ends
    
        echo 'Second largest number is : '. ($a < $b)? $a : $b;  // this is conditional operator and you can output the smallest of final two number 
    

    *The variable declaration and output is different but you can take the logic from it.

    0 讨论(0)
提交回复
热议问题