Google

May 3, 2014

Find the second highest number in an array

Core Java Coding Questions and Answers for beginner to intermediate level

Q1 Q2 Q3 Q4 Q5 - Q8 Q9 Q10 Q11 Q12 - Q14 Q15

Q. Can you find the second highest number in a given number?

For example: 

Input:  { 3, 5, 9, 7, 6, 8 };
Result: 8

A. A naive approach is to sort the array in descending order and then get the second element from the array. Sorting impacts performance, and  a better approach would be to

1) Have two variables to store highest & second highest numbers, and initialize them to minimum value.
2) Loop through each number in the array
3) If the "current number" > highest, make the current highest to second highest, and make the current value the highest.
4) else If the "current number < highest", but greater than the second highest then make "current number" the second highest.

public class SecondHighestNumber {

 public static void main(String[] args) {
  int[] numberArray = {3, 5, 9, 7, 6, 8};
        System.out.println(findSecondHighest(numberArray));
 }

 static int findSecondHighest(int[] numbers) {
  // initialize to the smallest value possible
  int highest = Integer.MIN_VALUE;
  int secondHighest = Integer.MIN_VALUE;

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

   //if highest number is found
   if (numbers[i] > highest) {
    secondHighest = highest; //make current highest to 2nd highest
    highest = numbers[i];    //make current value to highest
   } else if (numbers[i] > secondHighest)
    // replace the second highest
    secondHighest = numbers[i];
  }
  
  return secondHighest;
 }

}

Output:

8




Q. Is there anything wrong with the above code? What if you have duplicate numbers?

For example: int[] numberArray = {3, 5, 9, 7, 9, 8}; out put will be 9. If that is the expected behavior then it is fine. What if you want it to return 8 instead of 9?

A. Here is the slight revised solution that Ignores the equally highest numbers.

public class SecondHighestNumber {

 public static void main(String[] args) {
  int[] numberArray =  {3, 5, 9, 7, 9, 8};
        System.out.println(findSecondHighest(numberArray));
 }

 static int findSecondHighest(int[] numbers) {
  // initialize to the smallest value possible
  int highest = Integer.MIN_VALUE;
  int secondHighest = Integer.MIN_VALUE;

  for (int i = 0; i < numbers.length; i++) {
   
   //if highest or equally highest number is found
   //ignore the equally highest
   if (numbers[i] >= highest) {
    if(numbers[i] > highest) {
      secondHighest = highest; //make current highest to 2nd highest
      highest = numbers[i];    //make current value to highest
    }
   } else if (numbers[i] > secondHighest)
    // replace the second highest
    secondHighest = numbers[i];
  }
  
  return secondHighest;
 }

}

Output:

8

Labels: ,

2 Comments:

Anonymous Anonymous said...

Instead of updating the if condition, you could update the else condition to : else if (numbers[i] > secondHighest && numbers[i]!=highest)

6:42 PM, May 09, 2014  
Blogger Unknown said...

Good point Aafreen.

6:53 PM, May 09, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home