Watch 10 video solutions for First Unique Even Element, a easy level problem involving Array, Hash Table, Counting. This walkthrough by CodingHelp has 152 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 1001 <= nums[i] <= 100Problem 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.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Scan | O(n^2) | O(1) | Small arrays or quick prototype without extra memory |
| Hash Map Counting | O(n) | O(n) | General case and interview settings where linear time is expected |