Skip to main content

First Unique Even Element - Solution & Explanation

Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [3,4,2,5,4,6]

Output: 2

Explanation:

Both 2 and 6 are even and they appear exactly once. Since 2 occurs first in the array, the answer is 2.

Example 2:

Input: nums = [4,4]

Output: -1

Explanation:

No even integer appears exactly once, so return -1.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You are given an integer array and need to return the first even number that appears exactly once. The scan order matters: among all even numbers with frequency 1, return the one that appears earliest in the array.

This problem combines two simple tasks: filtering even numbers and checking their frequency. The key challenge is identifying uniqueness while preserving the original order of the array. Problems like this often rely on frequency tracking using a hash table or counting structure.

Approach 1: Brute Force Scan (O(n^2) time, O(1) space)

For every element in the array, first check if it is even. If it is, scan the entire array again and count how many times that value appears. If the count equals one, you found the first unique even element and can return it immediately. This approach performs a nested scan for each candidate element, which results in quadratic time complexity. It uses constant extra space because no auxiliary data structures are required. This method works for small inputs but becomes inefficient as the array size grows.

Approach 2: Hash Map Counting (O(n) time, O(n) space)

The optimal strategy uses frequency counting. First iterate through the array and record the frequency of each number using a hash map. Then iterate through the array again and check two conditions for each value: the number is even and its frequency equals one. The first element satisfying both conditions is the answer. Because hash map lookups run in constant time on average, the entire algorithm runs in linear time. This approach is a classic use of counting with a array traversal.

The key insight is separating counting from selection. The first pass gathers global frequency information, and the second pass preserves the original order while checking uniqueness. This avoids repeated scans and keeps the solution efficient.

Recommended for interviews: The hash map counting approach is what interviewers expect. It demonstrates that you recognize the pattern of using a frequency table to track occurrences in linear time. Mentioning the brute force method first shows baseline reasoning, but moving to the O(n) counting solution highlights optimization skills and familiarity with hash-based lookups.

Solution

We can use a hash table or array cnt to count the number of occurrences of each integer in the array. Then we traverse the array again to find and return the first even number that satisfies the condition. If no such even number exists, we return -1.

The time complexity is O(n), where n is the length of the array. The space complexity is O(M), where M is the range of integers in the array (100 in this problem).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ScanO(n^2)O(1)Small arrays or quick prototype without extra memory
Hash Map CountingO(n)O(n)General case and interview settings where linear time is expected

Video Solution

3866. First Unique Even Element | Biweekly Contest 178 | Leetcode • Rapid Syntax • 191 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is First Unique Even Element easy or hard?
First Unique Even Element is considered an easy problem. The main idea is recognizing the frequency counting pattern with a hash map and filtering for even numbers while preserving the array's original order.
First Unique Even Element Python/Java solution
Python implementations typically use a dictionary or collections.Counter to track frequencies, while Java solutions use a HashMap<Integer, Integer>. Both follow the same two-pass strategy: count occurrences first, then scan for the first even number with count equal to one.
How to solve First Unique Even Element in O(n)?
Use a hash map to store how many times each number appears. Perform a first pass through the array to build the frequency map. In a second pass, check each number in order and return the first element that is even and has frequency equal to one.
What is the best approach for First Unique Even Element?
The best approach uses a hash map to count frequencies of all numbers in the array. After building the frequency table in one pass, iterate through the array again and return the first value that is even and has a frequency of one. This method runs in O(n) time with O(n) extra space.
Is First Unique Even Element asked at Google/Amazon/Meta?
Problems based on frequency counting and hash maps are common in interviews at companies like Amazon, Google, and Meta. While the exact title may vary, identifying unique elements or first non-repeating values in an array appears frequently in screening rounds.
What data structure is used in First Unique Even Element?
A hash table (hash map or dictionary) is typically used to store element frequencies. This structure provides average O(1) insertion and lookup, which allows the algorithm to check whether a value is unique while scanning the array.
What is the time complexity of First Unique Even Element?
The optimal solution runs in O(n) time because the array is scanned twice: once to count frequencies and once to locate the first unique even value. Hash map lookups are O(1) on average, keeping the total runtime linear. Space complexity is O(n) for storing counts.

Ready to solve this problem?

Practice First Unique Even Element with our built-in code editor and test cases.

Practice on FleetCode