Skip to main content
Back to problems
#3866
Easy Algorithms

First unique even element

Array Hash Table Counting
66.3% acceptance
Mar 31, 2026
34
0
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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;

impl Solution {
  pub fn first_unique_even(nums: Vec<i32>) -> i32 {
    let mut count = HashMap::new();
    for &x in &nums {
      if x % 2 == 0 {
        *count.entry(x).or_insert(0) += 1;
      }
    }
    for &x in &nums {
      if x % 2 == 0 && count[&x] == 1 {
        return x;
      }
    }
    -1
  }
}