Difficulty: Easy | Acceptance: 66.30% | Paid: No Topics: Array, Hash Table, Counting
First Unique Even Element
Given an array of integers, find the first unique even element. A unique element is one that appears exactly once in the array. Return the first such element when traversing from left to right. If no such element exists, return -1.
Table of Contents
- Examples
- Constraints
- Brute Force
- Hash Map (Frequency Count)
- Linked Hash Map
Examples
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 <= 10⁴
-10⁹ <= nums[i] <= 10⁹
Brute Force
Intuition For each element in the array, check if it’s even and appears exactly once by counting its occurrences in the entire array.
Steps
- Iterate through each element in the array.
- For each element, check if it’s even (divisible by 2).
- If even, count its occurrences in the entire array.
- If the count is exactly 1, return this element.
- If no such element is found after checking all elements, return -1.
from typing import List
class Solution:
def firstUniqueEven(self, nums: List[int]) -> int:
for i in range(len(nums)):
if nums[i] % 2 == 0:
count = 0
for j in range(len(nums)):
if nums[j] == nums[i]:
count += 1
if count == 1:
return nums[i]
return -1Complexity
- Time: O(n²) - For each element, we traverse the entire array to count occurrences.
- Space: O(1) - We only use a constant amount of extra space.
- Notes: Simple but inefficient for large arrays.
Hash Map (Frequency Count)
Intuition First, count the frequency of each element using a hash map. Then, traverse the array again to find the first element that is even and has a frequency of 1.
Steps
- Create a hash map to store the frequency of each element.
- Iterate through the array and populate the hash map with element counts.
- Iterate through the array again.
- For each element, check if it’s even and has a frequency of 1 in the hash map.
- Return the first such element found.
- If no such element exists, return -1.
from typing import List
from collections import Counter
class Solution:
def firstUniqueEven(self, nums: List[int]) -> int:
freq = Counter(nums)
for num in nums:
if num % 2 == 0 and freq[num] == 1:
return num
return -1Complexity
- Time: O(n) - Two passes through the array, each O(n).
- Space: O(n) - Hash map stores at most n unique elements.
- Notes: Optimal solution with linear time complexity.
Linked Hash Map
Intuition Use a LinkedHashMap (or equivalent) to maintain insertion order while counting frequencies. This allows us to find the first unique even element in a single pass after building the map.
Steps
- Create a LinkedHashMap to store elements with their frequencies while maintaining insertion order.
- Iterate through the array and populate the map.
- Iterate through the map entries in insertion order.
- For each entry, check if the key is even and has a frequency of 1.
- Return the first such element found.
- If no such element exists, return -1.
from typing import List
from collections import OrderedDict
class Solution:
def firstUniqueEven(self, nums: List[int]) -> int:
freq = OrderedDict()
for num in nums:
freq[num] = freq.get(num, 0) + 1
for num, count in freq.items():
if num % 2 == 0 and count == 1:
return num
return -1Complexity
- Time: O(n) - Single pass to build the map and single pass to find the result.
- Space: O(n) - LinkedHashMap stores at most n unique elements.
- Notes: Similar to Hash Map approach but maintains insertion order, which can be useful in some scenarios.