简体   繁体   中英

Method to find second highest number in an array in java

Getting output of 0 each time when im ment to get 3 looked over my code my not sure where i have gone wrong i can do it without using a method i know but just trying to practice java

public class App {
    
    public static int second(int a[],int n) {
        int[] arr = new int [n];
        int temp;
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<n;j++) {
                if(arr[i] > arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr[n-2];
    }

    public static void main(String[] args) {
        int[] arr = {1,3,2,5,3};
        int n = 5;
        int result = second(arr,n);
        System.out.println(result);
    }
}

You could change the array parameter name arr and remove the declaration or copy the values from a to arr.

public static int second(int arr[],int n) {
    int temp;
    for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr[n-2];
}

The reason you get zero is because the primitive int cannot be null. So, when you create the array of length 5, it starts out filled with zeroes.

Doing it by using streams:

public static int second(int a[]) {
    return Arrays.stream(a)
            .sorted()
            .skip(a.length - 2)
            .findFirst()
            .getAsInt();
}

I removed the second argument. It sorts your array and skips all the elements prior to the one you want, before picking the now first element.

public static int second(int[] arr) {
    int highest = arr[0];
    int second = 0;
    for (int i = 1; i < arr.length; i++) {
        int j = arr[i];
        if (j >= highest) {
            highest = j;
        } else if (j > second) {
            second = j;
        }
    }
    return second;
}

public static void main(String[] args) {
    int[] arr = {1, 3, 2, 5, 3};
    int result = second(arr);
    System.out.println(result);
}

Here is one way that doesn't require sorting.

int[] arr = { 10, 2, 3, 19, 2, 3, 5 };
System.out.println(second(arr));

prints

10
  • set largest to the first value in the array
  • set secondLargest to the smallest possible
  • now iterate thru the array.
  • if the current value is greater than largest:
    • replace secondLargest with Largest
    • replace largest with current value
  • else check to see if current value is greater than secondLargest and assign if true.
public static int second(int arr[]) {
    int largest = arr[0];
    int secondLargest = Integer.MIN_VALUE;
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] > largest) {
            secondLargest = largest;
            largest = arr[i];
        } else if (arr[i] > secondLargest) {
            secondLargest = arr[i];
        }
    }
    return secondLargest;
}
public static int second(int arr[],int n) {
    int temp;
    if(arr.length < 2) {
        return -1;
    }
    else {
    for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr[n-2];
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int a[] = {23,14,56,77,66,67};
    int high = 0;
    int sec = 0;
    for(int i = 0 ; i <a.length; i++){
        if(high < a[i]){
            sec = high;
            high = a[i];
        }
        else if(sec < a[i]){
            sec = a[i];
        }
        
    }
    System.out.println("the first highest number is " + high);
    System.out.println("the second highest number is " + sec);
}

}

public static int sec(){
    int arr[] = {12,3,67,4,5,65};
    int high = 0;
    int low = 0;
    for(int i = 0 ; i < arr.length ; i ++){
        
        if(high < arr[i]){
            low = high;
            high = arr[i];
        }
        else if(low < arr[i]){
            low = arr[i];           
            }
    }
    return low;
}
public static void main(String[] args) {
    // TODO Auto-generated method stub

    sch obj = new sch();
    int a = obj.sec();
    
    System.out.println(a);
}

}

package com; public class FindSecondHighestNumberInArray {

public static void main(String[] args) {
    int []arrayOfIngeger = {-23,-989,-878,-2,-5,-3,-4,-123,-345,-98,-675,-98};
    int highestNumber = arrayOfIngeger[0];
    int secHighestNumber = 0;
    for(int i = 1; i < arrayOfIngeger.length; i++  ) {
        if(highestNumber < arrayOfIngeger[i]) {
            System.out.println(highestNumber < arrayOfIngeger[i]);              
            secHighestNumber = highestNumber; 
            highestNumber = arrayOfIngeger[i];  
        }else if(secHighestNumber < arrayOfIngeger[i]) {
            secHighestNumber = arrayOfIngeger[i];
        }
    }
    System.out.println("second Highest Number in Array   :  "+secHighestNumber);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM