↓Skip to main content
  1. LeetCode/

LeetCode 3843: First Element with Unique Frequency

·1 min· ·
LeetCode Medium Hash-Table Counting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-16

  • Submissions: Accepted (first try)

Approach
#

Two levels of counting (Hash Table Template 1):

  • cnt: value -> count.
  • freq_of_freq: count -> how many distinct values have that count.

“Unique count” means freq_of_freq[cnt[x]] == 1. For the first one, scan the original array from the left.

My Solution
#

class Solution:
    def firstUniqueFreq(self, nums: List[int]) -> int:
        cnt = Counter(nums)
        freq_of_freq = Counter(cnt.values())
        for x in nums:
            if freq_of_freq[cnt[x]] == 1:
                return x
        return -1

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • “First” follows the original array’s order, not cnt’s.

Interview Explanation
#

I count each value, then count how many values share each count. Scanning the original array from the left, the first element whose count belongs to only one value is the answer. Time O(n).

Related

LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·2 mins
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 note: count each value on and off the Y, enumerate the Y value a and background value b (a != b), and take the fewest cells to change
LeetCode 229: Majority Element II
·2 mins
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 note: at most two values can exceed n / 3; Boyer-Moore keeps two candidates and a second pass verifies their counts
LeetCode 3719: Longest Balanced Subarray I
·2 mins
LeetCode Medium Hash-Table Prefix-Sum Segment-Tree Array
LeetCode 3719 note: n is only 1500, so fix the left end and extend right, maintaining distinct odds and evens in two sets