Data Structures & AlgorithmsVery hard

When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership tests?

  1. A. A hash set of values seen so far, scanning the list once.
  2. B. Sorting the list and comparing adjacent values only.
  3. C. Recursively comparing each node with every node after it.
  4. D. A binary search tree of positions.

Correct answer: A. A hash set of values seen so far, scanning the list once.

A hash set answers 'seen before?' in expected O(1) while the list is traversed in its original order. Sorting would destroy the original order that the problem asks to keep.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic