```
public int ThirdMax(int[] nums) {
if (nums == null) return -1;
Array.Sort(nums);
var decrementCount = 2;
for (int i = nums.Length - 1; i > 0; i--)
{
if (nums[i] != nums[i - 1])
{
decrementCount --;
if (decrementCount == 0)
{
return nums[i-1];
}
}
}
if (decrementCount != 0) return nums[nums.Length - 1];
return -1;
}
```