Hackbright Code Challenges

Remove Duplicates: Solution

Remove Duplicates: Solution

Problem

Remove Duplicates

Whiteboard

Easier

Challenge

Easier

Concepts

Lists, Sets

Download

remove-duplicates-solution.zip


Sets are great for de-duplicating lists:

>>> deduped = set([1, 1, 2, 3]))
>>> answer = list(deduped)

Except that sets do not maintain order, so we can’t extract the original order where items appeared; our answer could be [2, 1, 3], when we want [1, 2, 3].

You need to do the de-duplicating by hand. You could do this like:

result = []

for char in items:
    if char not in result:
        result.append(char)

return result

This would work, but it would be O(n ** 2) runtime, since, for every item, we’d be looking in the result list (which is a linear operation).

You can solve this by using a set to keep track of what’s seen, and using a list to hold the results:

dedupe.py
def deduped(items):
    """Return new list from items with duplicates removed."""
    
    # START SOLUTION

    # keep track of items we've seen
    seen = set()

    # list to hold our answer
    result = []

    for item in items:
        if item not in seen:
            result.append(item)
            seen.add(item)

    return result