Answers for "find top 2 values in array list"

0

find top 2 values in array list

public static void twoLargest(int values[]){

    int largestA = values[0];
    int largestB = -1;

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

            if(values[i] > largestA){
                largestB = largestA;
                largestA = values[i];
            }
            else if (values[i] > largestB && values[i] != largestA) {
                largestB = values[i];
            }
    }
    System.out.println("Largest - " + largestA);
    System.out.println("2nd largest Largest - " + largestB);
}
Posted by: Guest on August-26-2020

Code answers related to "find top 2 values in array list"

Browse Popular Code Answers by Language