class Solution {
public int thirdMax(int[] nums) {
int n = nums.length;
long first, second, third;
first = second = third = Long.MIN_VALUE;
if(n == 1) return nums[0];
if(n == 2) return Math.max(nums[0], nums[1]);
for(int i = 0; i < n; i++) {
if(nums[i] == first || nums[i] == second) continue;
if(nums[i] > first) {
third = second;
second = first;
first = nums[i];
continue;
}
if(nums[i] > second) {
third = second;
second = nums[i];
continue;
}
if(nums[i] > third) {
third = nums[i];
}
}
return third == Long.MIN_VALUE ? (int)first : (int)third;
}
}