```
class Solution(object):
def thirdMax(self, a):
x, y, z = a[0], float('-inf'), float('-inf')
for e in a:
if e > x: x, y, z = e, x, y
elif y < e < x: y, z = e, y
elif z < e < y: z = e
return z if x != y != z and z != float('-inf') else x
```