1
class Solution {
2
public int thirdMax(int[] nums) {
3

4
// taking long data type since array can contain Integer.MIN_VALUE
5
long max = Long.MIN_VALUE, sMax = Long.MIN_VALUE, tMax = Long.MIN_VALUE;
6
for (int i : nums) {
7
if (i > max) {
8
tMax = sMax;
9
sMax = max;
10
max = i;
11
} else if (i > sMax && i != max) {
12
tMax = sMax;
13
sMax = i;
14
} else if (i > tMax && i != sMax && i != max) {
15
tMax = i;
16
}
17
}
18

19
// if thirdMax was not updated, return the first Max
20

21
return tMax == Long.MIN_VALUE ? (int) max : (int) tMax;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0