-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtils.java
More file actions
30 lines (25 loc) · 779 Bytes
/
ArrayUtils.java
File metadata and controls
30 lines (25 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ArrayUtils {
public static int findSecondLargest(int[] arr) {
if (arr == null || arr.length < 2) {
return -1;
}
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) {
return -1;
}
return secondLargest;
}
public static void main(String[] args) {
int[] numbers = {4, 1, 6, 3, 6};
System.out.println(findSecondLargest(numbers));
}
}