Skip to main content

First Element with Unique Frequency - Solution & Explanation

MediumArrayHash TableCounting6 min read
Practice this problem

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 <= 105
  • 1 <= 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Frequency ComparisonO(n²)O(n)Useful for understanding the problem or when input size is very small
Hash Table + Frequency-of-FrequencyO(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?
This problem is usually classified as Medium difficulty. The main challenge is recognizing that checking frequency uniqueness efficiently requires an additional frequency-of-frequency map rather than repeatedly scanning all counts.
First Element with Unique Frequency Python/Java solution
In Python, a dictionary or collections.Counter can track frequencies, followed by another dictionary for frequency counts. In Java, HashMap<Integer, Integer> is typically used for both maps. The same logic applies in C++, Go, and TypeScript using their standard hash map structures.
How to solve First Element with Unique Frequency in O(n)?
Use a two-step counting approach. First create a hash map that stores how many times each number appears. Then build another map that counts how many numbers have each frequency. Finally iterate through the array and return the first element whose frequency count equals one.
What is the best approach for First Element with Unique Frequency?
The best approach uses two hash tables. First count the frequency of each element, then count how many elements share each frequency. After that, scan the array in its original order and return the first element whose frequency occurs exactly once. This runs in O(n) time with O(n) extra space.
Is First Element with Unique Frequency asked at Google/Amazon/Meta?
Frequency counting and hash map problems are common in interviews at companies like Amazon, Google, and Meta. Variations of this problem appear when testing understanding of hash tables, counting strategies, and handling order-sensitive array scans.
What data structure is used in First Element with Unique Frequency?
The primary data structure is a hash table (hash map). One map stores element frequencies, and another tracks how many elements share each frequency. Arrays are used for iteration order, while the hash maps provide constant-time lookups.
What is the time complexity of First Element with Unique Frequency?
The optimal solution runs in O(n) time. One pass builds the element frequency map, another pass builds the frequency-of-frequency map, and a final scan checks elements in order. Each step uses constant-time hash lookups, keeping the overall complexity linear.

Ready to solve this problem?

Practice First Element with Unique Frequency with our built-in code editor and test cases.

Practice on FleetCode