Largest Unique Number - Problem
You're given an array of integers, and your mission is to find the largest number that appears exactly once. Think of it as finding the biggest "loner" in the crowd!
Here's what makes this interesting: a number might appear multiple times in the array, but you only care about those special numbers that appear exactly once. Among all these unique numbers, you want the largest one.
The Challenge:
- If there are numbers that appear exactly once, return the largest of them
- If every number appears more than once (no unique numbers), return
-1
Example: In [5, 7, 3, 9, 4, 9, 8, 3, 1], the numbers 5, 7, 4, 8, 1 appear exactly once. The largest among these unique numbers is 8.
Input & Output
example_1.py โ Python
$
Input:
[2, 3, 5, 5]
โบ
Output:
3
๐ก Note:
The unique numbers are 2 and 3. The largest unique number is 3.
example_2.py โ Python
$
Input:
[5, 7, 3, 9, 4, 9, 8, 3, 1]
โบ
Output:
8
๐ก Note:
Numbers 5, 7, 4, 8, 1 appear exactly once. Among these, 8 is the largest.
example_3.py โ Python
$
Input:
[9, 9, 8, 8]
โบ
Output:
-1
๐ก Note:
All numbers appear more than once, so there are no unique numbers. Return -1.
Constraints
- 1 โค nums.length โค 1000
- 0 โค nums[i] โค 1000
- Follow up: Can you solve this in O(n) time?
Visualization
Tap to expand
Understanding the Visualization
1
Scan the List
Go through each name and count how many times they appear
2
Track Solo Guests
Keep track of the highest ID among guests who appear exactly once
3
Handle Duplicates
When someone appears multiple times, they're no longer a solo guest
4
Return Result
The highest ID among solo guests is our answer
Key Takeaway
๐ฏ Key Insight: Use a hash map to count frequencies in O(n) time, while simultaneously tracking the maximum among unique elements. This avoids the need for nested loops and achieves optimal performance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code