Exercise 2 solution def dedupe(items): seen = set() out = [] for x in items: if x not in seen: seen.add(x) out.append(x) return out print(dedupe([3, 1, 3, 2, 1])) # [3, 1, 2] Two structures: - a set (seen) for the "have I met this already?" test, which is one hash lookup per item instead of a scan of everything kept so far; - a list (out) to keep the first-seen order, because a set has no order. Checking "x not in out" instead of using the set would work but would be O(n squared) overall, which becomes very slow on long inputs. Shorter alternative (values must be hashable): list(dict.fromkeys(items)), since a dict keeps insertion order and its keys are unique.