```
public int thirdMax(int[] nums) {
// Sort Array in ascending order.
Arrays.sort(nums);
int length = nums.length;
if(length == 2) return nums[length-1];
int count = 0;
int curr = 0;
int prev = 0;
// If count==3 then you have reached the 3rd highest number.
// if i reaches till the very
//beginning and still count is not 3 then return the highest number.
for(int i=length-1; i>=0; i--){
prev = curr;
curr = nums[i];
if(curr != prev) count++;
if(count == 3) break;
else if(i==0) curr = nums[length-1];
}
return curr;
}
}```
```