# Unique Values

> Duplicates are noise. Remove them.

Canonical URL: <https://datadriven.io/problems/unique_values>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list, return a new list containing only the first occurrence of each distinct value, preserving input order.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **order-preserving deduplication**, a fundamental data cleaning operation. It probes whether a candidate uses a set for O(1) lookups while maintaining insertion order with a list.

---

### Break down the requirements

#### Step 1: Track seen elements with a set

For O(1) membership checks.

#### Step 2: Collect first occurrences in order

Append to the result list only if the element has not been seen before.

---

### The solution

**Seen-set with order preservation**

```python
def unique_values(items: list) -> list:
    seen = set()
    result = []
    for item in items:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) with O(1) average set operations.
> 
> **Space:** O(n) for the seen set and result list.

> **Interviewers Watch For**
>
> Using `dict.fromkeys(items)` is a Python trick that preserves order (Python 3.7+), but interviewers usually want to see the manual implementation.

> **Common Pitfall**
>
> Using `list(set(items))` loses the original order since sets are unordered.

---

## Common follow-up questions

- What if elements were unhashable? _(Tests falling back to a list for seen tracking (O(n^2)) or serializing elements.)_
- How would you deduplicate by a key function? _(Tests using `key(item)` for the seen set while collecting the original items.)_
- What is the SQL equivalent? _(Tests `SELECT DISTINCT` or `GROUP BY` with ordering.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/unique_values)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.