Data Structures & AlgorithmsModerateTCS NQT · Arrays

The sorted array [1, 1, 2, 2, 2, 3, 5, 5, 8] is de-duplicated in place by two pointers: a write index w starts at 1 and each element a[i] (i from 1) that differs from a[w-1] is copied to a[w] and w is incremented. What is the final value of w?

  1. A. 4
  2. B. 6
  3. C. 7
  4. D. 5

Correct answer: D. 5

Distinct values are 1, 2, 3, 5, 8, so five slots are filled and w = 5.

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

Exams that ask this topic