ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints the number of distinct values in the merged list. What is printed?

Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints the number of distinct values in the merged list. What is printed?
merge the two sorted lists into one sorted list
count = 0; prev = none
for each x in merged
    if x != prev then count = count + 1; prev = x
end for
Print count
  1. A. 6
  2. B. 1
  3. C. 5
  4. D. 4

Correct answer: C. 5

The merged list is 1, 2, 3, 3, 5, 6, with the value 3 repeated, so there are 5 distinct values. Counting all elements gives 6, distinct values minus one gives 4, and a broken comparison reports a different count.

More Pseudocode and Code Tracing questions

Exams that ask this topic