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
Asked
Active
Viewed 2,267 times
0
-
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 Answers
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];
}
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