Drill

ProblemsPython › data

Drop the repeats

easydataPython

A feed sometimes sends the same id twice, and a clean list keeps only the first sighting.

dedupe(values: list<int>) → list<int>

Solve it in the editor →

Where you start

def dedupe(values: list[int]) -> list[int]:
    

Worked examples

CallResult
dedupe([1, 2, 3, 1, 2])[1, 2, 3]
dedupe([1, 1, 1])[1]
dedupe([])[]
dedupe([3, 1, 2, 1, 3])[3, 1, 2]

Hint

Carry a set of what you have already seen, and only push a value the first time you meet it.

Reference solution in Python
def dedupe(values: list[int]) -> list[int]:
    seen = set()
    result = []
    for v in values:
        if v not in seen:
            seen.add(v)
            result.append(v)
    return result

The same problem in another language

More data problems in Python