Skip to main content

First Element with Unique Frequency - Video Solutions

MediumArrayHash TableCounting

First Element with Unique Frequency | LeetCode 3843 | Weekly Contest 489 | Java | Developer Coder

Developer Coder
8:28287 views
9 video solutions available

First Element with Unique Frequency - Video Solution

Watch 9 video solutions for First Element with Unique Frequency, a medium level problem involving Array, Hash Table, Counting. This walkthrough by Developer Coder has 287 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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