Skip to content

Latest commit

 

History

History
14 lines (11 loc) · 469 Bytes

414. Third Maximum Number.md

File metadata and controls

14 lines (11 loc) · 469 Bytes

Solution:

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

        nums=list(dict.fromkeys(nums))  #to remove duplicates
        nums.sort()                     #to easily navigate to the large numbers

        if len(nums)<3:
            return nums[-1]             #if there is not 3 elements, preventing existence of a 3rd largest
        else:
            return nums[-3]             #if there will exist a 3rd largest since n>=3