DEV Community

SalahElhossiny
SalahElhossiny

Posted on

2 1

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)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay