3843. First Element with Unique Frequency Medium

@problem@discussion
#Array#Hash Table#Counting



1/**
2 * [3843] First Element with Unique Frequency
3 *
4 * You are given an integer array nums.
5 * 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.
6 *  
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [20,10,30,30]</span>
10 * Output: <span class="example-io">30</span>
11 * Explanation:
12 * 
13 * 	20 appears once.
14 * 	10 appears once.
15 * 	30 appears twice.
16 * 	The frequency of 30 is unique because no other integer appears exactly twice.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [20,20,10,30,30,30]</span>
21 * Output: <span class="example-io">20</span>
22 * Explanation:
23 * 
24 * 	20 appears twice.
25 * 	10 appears once.
26 * 	30 appears 3 times.
27 * 	The frequency of 20, 10, and 30 are unique. The first element that has unique frequency is 20.
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">nums = [10,10,20,20]</span>
32 * Output: <span class="example-io">-1</span>
33 * Explanation:
34 * 
35 * 	10 appears twice.
36 * 	20 appears twice.
37 * 	No element has a unique frequency.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	1 <= nums.length <= 10^5
43 * 	1 <= nums[i] <= 10^5
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/first-element-with-unique-frequency/
49// discuss: https://leetcode.com/problems/first-element-with-unique-frequency/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn first_unique_freq(nums: Vec<i32>) -> i32 {
55        0
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_3843() {
67    }
68}
69

Back
© 2026 bowen.ge All Rights Reserved.