DEV Community

SalahElhossiny
SalahElhossiny

Posted on

Third Maximum Number

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.


class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        n = list(set(nums))

        if len(n) < 3:
            return max(n)

        return sorted(n)[-3]




Enter fullscreen mode Exit fullscreen mode

Top comments (0)