0

How to get second maximum value from array without using nested loops in java? For eg: int [] arr = {2,4,1,6,5,9,0,7} Output : 7

  • Just posted an answer to the question "sort an array without using loops", but have deleted it as I realise that's not actually what you're asking. Might be sensible to update the question title. – HappyDog Apr 01 '17 at 11:59

3 Answers3

0

Try this:

import java.util.Arrays;
...
Arrays.sort(arr);
result = arr[arr.length - 2];
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
HappyDog
  • 1,230
  • 1
  • 18
  • 45
0
if (arr.length > 1)
{
    Collections.Arrays.sort(arr);
    int secondMax = arr[arr.length - 2];
}

Inspired by this question.

Community
  • 1
  • 1
Justin K
  • 239
  • 2
  • 13
0

You can use array sort method easily.

int [] arr = {2,4,1,6,5,9,0,7};
Arrays.sort(arr);
System.out.println(arr[arr.length - 2]);
Emalsha Rasad
  • 328
  • 3
  • 11