[LeetCode] First Unique Even Element

3866. First Unique Even Element

You are given an integer array nums.

Return an integer denoting the first even integer (earliest by array index) that appears exactly once in nums. If no such integer exists, return -1.

An integer x is considered even if it is divisible by 2.

1
2
3
4
5
6
7
8
9
class Solution {
public:
int firstUniqueEven(vector<int>& nums) {
unordered_map<int,int> freq;
for(auto& n : nums) freq[n]++;
for(auto& n : nums) if(freq[n] == 1 and n % 2 == 0) return n;
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/first-unique-even-element/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.