ProgrammingHardProduct-company MCQ pattern

Which approach removes duplicates from a list while keeping the first-seen order, in O(n) average time?

  1. A. list(set(items))
  2. B. sorted(set(items), key=items.index)
  3. C. [x for x in items if x not in set(items)]
  4. D. list(dict.fromkeys(items))

Correct answer: D. list(dict.fromkeys(items))

dict keys are unique and keep insertion order, so fromkeys does one hash insert per item. set() loses the order, and the other two options are O(n squared) or worse.

More Python Programming questions

Exams that ask this topic