First Unique Even Element

2026. 3. 15. 14:36Algorithm/Leetcode, Lintcode, HackerRank, etc.

    목차
반응형

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

class Solution:
    def firstUniqueEven(self, nums: list[int]) -> int:
        freq = collections.defaultdict(int)
        removes = set()
        evens = []

        for i, num in enumerate(nums):
            if num % 2 == 0:
                freq[num] += 1
                evens += num,

            if freq[num] > 1:
                removes.add(num)

        for remove in removes:
            del freq[remove]

        for even in evens:
            if even in freq:
                return even

        return -1
반응형

'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글

Minimum cost to equalize arrays using swap  (0) 2026.03.15
Sum of GCD of Formed Pairs  (0) 2026.03.15
Count commas in range II  (0) 2026.03.15
Count Commas in Range  (0) 2026.03.15
Longest Univalve path  (0) 2026.03.15