Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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:
Example 2:
Input: nums = [20,20,10,30,30,30]
Output: 20
Explanation:
Example 3:
Input: nums = [10,10,20,20]
Output: -1
Explanation:
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[20,10,30,30]
[20,20,10,30,30,30]
[10,10,20,20]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3843: First Element with Unique Frequency
class Solution {
public int firstUniqueFreq(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
Map<Integer, Integer> freq = new HashMap<>();
for (int v : cnt.values()) {
freq.merge(v, 1, Integer::sum);
}
for (int x : nums) {
if (freq.get(cnt.get(x)) == 1) {
return x;
}
}
return -1;
}
}
// Accepted solution for LeetCode #3843: First Element with Unique Frequency
func firstUniqueFreq(nums []int) int {
cnt := make(map[int]int)
for _, x := range nums {
cnt[x]++
}
freq := make(map[int]int)
for _, v := range cnt {
freq[v]++
}
for _, x := range nums {
if freq[cnt[x]] == 1 {
return x
}
}
return -1
}
# Accepted solution for LeetCode #3843: First Element with Unique Frequency
class Solution:
def firstUniqueFreq(self, nums: List[int]) -> int:
cnt = Counter(nums)
freq = Counter(cnt.values())
for x in nums:
if freq[cnt[x]] == 1:
return x
return -1
// Accepted solution for LeetCode #3843: First Element with Unique Frequency
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3843: First Element with Unique Frequency
// class Solution {
// public int firstUniqueFreq(int[] nums) {
// Map<Integer, Integer> cnt = new HashMap<>();
// for (int x : nums) {
// cnt.merge(x, 1, Integer::sum);
// }
// Map<Integer, Integer> freq = new HashMap<>();
// for (int v : cnt.values()) {
// freq.merge(v, 1, Integer::sum);
// }
// for (int x : nums) {
// if (freq.get(cnt.get(x)) == 1) {
// return x;
// }
// }
// return -1;
// }
// }
// Accepted solution for LeetCode #3843: First Element with Unique Frequency
function firstUniqueFreq(nums: number[]): number {
const cnt = new Map<number, number>();
for (const x of nums) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}
const freq = new Map<number, number>();
for (const v of cnt.values()) {
freq.set(v, (freq.get(v) ?? 0) + 1);
}
for (const x of nums) {
if (freq.get(cnt.get(x)!) === 1) {
return x;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.