Q:

Find the second largest number in an integer array. You can assume a minimum array length of two

0

Find the second largest number in an integer array. You can assume a minimum array length of two.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public Integer find(Integer[] arr) {
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;

for (int i = 0; i < arr.length; i++) {
  if (arr[i] > max) {
    secondMax = max;
    max = arr[i];
  }

  if (arr[i] < max && arr[i] > secondMax) {
    secondMax = arr[i];
  }
}
return secondMax;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now