First Element with Unique Frequency - Solution & Explanation
Problem Statement
You are given an integer array nums.
Return an integer denoting the first element (scanning from left to right) in nums whose frequency is unique. That is, no other integer appears the same number of times in nums. If there is no such element, return -1.
Example 1:
Input: nums = [20,10,30,30]
Output: 30
Explanation:
- 20 appears once.
- 10 appears once.
- 30 appears twice.
- The frequency of 30 is unique because no other integer appears exactly twice.
Example 2:
Input: nums = [20,20,10,30,30,30]
Output: 20
Explanation:
- 20 appears twice.
- 10 appears once.
- 30 appears 3 times.
- The frequency of 20, 10, and 30 are unique. The first element that has unique frequency is 20.
Example 3:
Input: nums = [10,10,20,20]
Output: -1
Explanation:
- 10 appears twice.
- 20 appears twice.
- No element has a unique frequency.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105
Approach Overview
Problem Overview: You are given an array of integers and need to return the first element whose frequency is unique compared to all other element frequencies. If multiple values appear different numbers of times, only the one whose frequency occurs exactly once should be returned, while also respecting the element's first appearance order in the array.
Approach 1: Brute Force Frequency Comparison (O(n²) time, O(n) space)
Start by computing the frequency of every element using a hash map. Then iterate through the array again and, for each element, check whether any other element has the same frequency. This requires scanning the frequency map for every candidate. If no other element shares that frequency, you found the answer. The approach is straightforward but inefficient because each frequency check can scan up to n entries, leading to quadratic behavior for large inputs.
Approach 2: Hash Table with Frequency-of-Frequency Counting (O(n) time, O(n) space)
The optimal solution uses two hash tables. First, build a frequency map where each number maps to its occurrence count. Next, build a second map that counts how many elements have each frequency. This converts the "is this frequency unique?" check into a constant-time lookup. Finally, iterate through the original array order and return the first element whose frequency appears exactly once in the frequency-count map. This approach relies heavily on constant-time hash lookups and avoids repeated scans.
This pattern appears frequently in problems involving frequency analysis and counting structures. Using a hash table makes counting efficient, while the second map handles uniqueness checks in constant time. The input traversal preserves the "first element" requirement, which is common in array problems where order matters. The counting strategy is also a classic example of counting techniques used in many interview problems.
Recommended for interviews: Interviewers expect the hash table counting solution. The brute force approach demonstrates you understand the definition of frequency uniqueness, but the optimized two-map strategy shows you know how to reduce repeated work and achieve O(n) time. Always mention that the array is scanned twice—once for building counts and once for preserving order—while hash lookups keep each operation constant time.
Solution
We use a hash table cnt to count the occurrences of each element, and then use another hash table freq to count the frequency of each occurrence count. Finally, we traverse the array nums again. For each element x, if the value of freq[cnt[x]] is 1, it means the occurrence frequency of x is unique, and we return x. If no such element is found after traversing, return -1.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Frequency Comparison | O(n²) | O(n) | Useful for understanding the problem or when input size is very small |
| Hash Table + Frequency-of-Frequency | O(n) | O(n) | Best general solution; efficient for large arrays and expected in interviews |
Video Solution
First Element with Unique Frequency | LeetCode 3843 | Weekly Contest 489 | Java | Developer Coder • Developer Coder • 287 views views
Watch 8 more video solutions →Frequently Asked Questions
Is First Element with Unique Frequency easy or hard?
First Element with Unique Frequency Python/Java solution
How to solve First Element with Unique Frequency in O(n)?
What is the best approach for First Element with Unique Frequency?
Is First Element with Unique Frequency asked at Google/Amazon/Meta?
What data structure is used in First Element with Unique Frequency?
What is the time complexity of First Element with Unique Frequency?
Ready to solve this problem?
Practice First Element with Unique Frequency with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor