< Back




1133. Largest Unique Number

Pretty straightforward question. Given an array of integers, find the greatest number that only
occurs once. The optimal solution involves using the Counter class from the Python collections
library.

Once we counted all the occurrences of each number in the input array, we iterate through the
dictionary and filter out numbers that have an occurrence count greater than 1. We do this by only
inspecting numbers whose occurrence count == 1 - from there we use max() to store the larget number
in the answer variable.

The solution is as follows:


  from collections import Counter

  class Solution:
      def largestUniqueNumber(self, nums: List[int]) -> int:
          seen = Counter(nums)
          ans = -1

          for num, count in seen.items():
              if count == 1:
                  ans = max(ans, num)

          return ans


_ Time Complexity:

  O(n) - We count all occurrences of each number in the input array.

_ Space Complexity:

  O(n) - We store all occurrences of each number in the input array in a dictionary.