Skip to content

S2Q1 · Count Unique Even and Odd Numbers

⚡ Quick Reference

Function: count_unique_even_odd(l: list) -> dict

Core idea: deduplicate with set(), then count even and odd separately.

def count_unique_even_odd(l: list) -> dict:
    unique = set(l)
    return {
        "even": sum(1 for x in unique if x % 2 == 0),
        "odd":  sum(1 for x in unique if x % 2 != 0)
    }

Key rules: - Remove duplicates first with set(l) before counting - Even → x % 2 == 0, Odd → x % 2 != 0 - Return a dict with keys "even" and "odd"


Problem Statement

Problem

Write a function count_unique_even_odd(l: list) -> dict that returns a dictionary with two keys - "even" and "odd" - where the values are the counts of unique even and odd numbers in the list respectively.

Example:

Input
l = [1, 2, 2, 3, 4, 5, 5, 6, 9]
Output
{"even": 3, "odd": 4}

Understanding the problem

The key word is unique - duplicates must be ignored before counting.

l = [1, 2, 2, 3, 4, 5, 5, 6, 9]

After deduplication:  {1, 2, 3, 4, 5, 6, 9}

Even unique: 2, 4, 6        → count = 3
Odd  unique: 1, 3, 5, 9     → count = 4

Deduplicate first, then count

If you count directly from the list, 2 and 5 would each be counted twice. Converting to a set first eliminates all duplicates in one step, then you count on the clean unique values.


Tracing the example

l = [1, 2, 2, 3, 4, 5, 5, 6, 9]

Step Operation Result
Deduplicate set(l) {1, 2, 3, 4, 5, 6, 9}
Even check x % 2 == 0 for each 2, 4, 6 → count = 3
Odd check x % 2 != 0 for each 1, 3, 5, 9 → count = 4
Return dict {"even": 3, "odd": 4}

Solution approaches

def count_unique_even_odd(l: list) -> dict:
    unique = set(l)
    return {
        "even": sum(1 for x in unique if x % 2 == 0),
        "odd":  sum(1 for x in unique if x % 2 != 0)
    }

set(l) deduplicates, sum(1 for x in ... if condition) counts. Clean and idiomatic.

def count_unique_even_odd(l: list) -> dict:
    unique = set(l)
    even_count = 0
    odd_count  = 0
    for x in unique:
        if x % 2 == 0:
            even_count += 1
        else:
            odd_count += 1
    return {"even": even_count, "odd": odd_count}

Walk through the unique values explicitly - most readable for beginners.

def count_unique_even_odd(l: list) -> dict:
    unique = set(l)
    return {
        "even": len([x for x in unique if x % 2 == 0]),
        "odd":  len([x for x in unique if x % 2 != 0])
    }

Build filtered lists with comprehensions and take their length. Slightly less memory-efficient than sum(1 for ...) since it materialises the full list, but equally readable.

def count_unique_even_odd(l: list) -> dict:
    unique = set(l)
    return {
        "even": len(list(filter(lambda x: x % 2 == 0, unique))),
        "odd":  len(list(filter(lambda x: x % 2 != 0, unique)))
    }

filter(condition, iterable) returns an iterator of matching elements. Wrap in list() then take len(). More verbose than comprehensions but shows functional programming style.


Common mistake - counting before deduplicating

# ❌ Wrong - counts duplicates
even_count = sum(1 for x in l if x % 2 == 0)   # counts 2 twice
odd_count  = sum(1 for x in l if x % 2 != 0)    # counts 5 twice
# ✅ Correct - deduplicate first
unique = set(l)
even_count = sum(1 for x in unique if x % 2 == 0)

Always convert to set before counting when the problem asks for unique counts.


Key takeaways

01

set() to deduplicate before counting

Whenever a problem says "unique", convert to set() first. All subsequent operations on the set automatically work on unique values only.

02

sum(1 for x in iterable if cond)

The idiomatic way to count items satisfying a condition. More memory-efficient than building a list and calling len().

03

even + odd = total unique

Every integer is either even or odd - never both, never neither. So even_count + odd_count always equals len(set(l)). Useful for a quick sanity check.