Problem
Given an integer arraynums, return the largest integer that only occurs once. If no integer occurs once, return -1.
Examples
Constraints
- 1 <= nums.length <= 2000
- 0 <= nums[i] <= 1000
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 1133 with 20 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 1133 # by problem number
lcpy gen -s largest_unique_number # by problem name
nums, return the largest integer that only occurs once. If no integer occurs once, return -1.
Input: nums = [5,7,3,9,4,9,8,3,1]
Output: 8
Input: nums = [9,9,8,8]
Output: -1
from collections import Counter
class Solution:
# Time: O(n)
# Space: O(n)
def largest_unique_number(self, nums: list[int]) -> int:
counts = Counter(nums)
candidates = [value for value, count in counts.items() if count == 1]
return max(candidates) if candidates else -1
| Time | Space |
|---|---|
| O(n) | O(n) |