```
public int thirdMax(int[] nums) {
Arrays.sort(nums);
int count = 1, i = nums.length-1;
while(i > 0) {
while(i > 0 && nums[i] == nums[--i]);
if(i == 0 && nums[i] == nums[i+1]) break;
count++;
if(count == 3) break;
}
return count < 3 ? nums[nums.length-1] : nums[i];
}
```