Largest Unique Number - Problem

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

An integer occurs once if it appears exactly one time in the array, meaning it has no duplicates.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,3,2,1]
Output: 3
💡 Note: Numbers 2 and 1 each appear twice, but 3 appears only once. Since 3 is the largest unique number, return 3.
Example 2 — No Unique Numbers
$ Input: nums = [1,1,2,2]
Output: -1
💡 Note: Every number appears exactly twice, so there are no unique numbers. Return -1.
Example 3 — Single Element
$ Input: nums = [5]
Output: 5
💡 Note: The array has only one element, which appears exactly once, so return 5.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Find Largest Unique NumberInput Array:21321Frequency Count:2 appears 2×1 appears 2×3 appears 1×Only number 3 appears exactly onceOutput: 33 is the largest (and only) unique number
Understanding the Visualization
1
Input Array
Array with some duplicate numbers
2
Count Frequencies
Identify which numbers appear exactly once
3
Find Maximum
Return the largest unique number
Key Takeaway
🎯 Key Insight: Use frequency counting to identify unique elements, then find the maximum among them
Asked in
Amazon 25 Google 18 Facebook 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen