Largest Unique Number — LeetCode 1133 Python Solution

EasyLeetCode PremiumArrayHash TableSorting
Problem
#1133
Pattern
Sorting
Reading time
2 min

The problem

Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.

Example

Input
nums = [5,7,3,9,4,9,8,3,1]
Output
8
Explanation
The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it is the answer.

Python solution

Python
class Solution:
    def largestUniqueNumber(self, nums: List[int]) -> int:
        cnt = Counter(nums)
        return max((x for x, v in cnt.items() if v == 1), default=-1)

Complexity

MeasureComplexity
TimeO(n + M)
SpaceO(M) auxiliary

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1133. Largest Unique Number is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1133. Largest Unique Number?
LeetCode 1133. Largest Unique Number is rated Easy on LeetCode.
What is the time complexity of LeetCode 1133. Largest Unique Number?
The Python solution on this page runs in O(n + M).
What is the space complexity of LeetCode 1133. Largest Unique Number?
The Python solution on this page uses O(M) auxiliary space.
What topics does LeetCode 1133. Largest Unique Number cover?
LeetCode 1133. Largest Unique Number is tagged Array, Hash Table and Sorting on LeetCode.
Is LeetCode 1133. Largest Unique Number a premium problem?
Yes. LeetCode 1133. Largest Unique Number is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview