ProgrammingModerate

To find duplicate keys among 2 x 10^5 records, which method is correct and fast enough?

  1. A. Compare every pair of records with nested loops
  2. B. Call bsearch for each record on an unsorted array
  3. C. Linear search for each record in a copy of the array
  4. D. Sort the records by key, then compare each record with its neighbour

Correct answer: D. Sort the records by key, then compare each record with its neighbour

After sorting, equal keys are adjacent, so one linear scan finds duplicates in O(N log N) in total. Nested loops would take about 2 x 10^10 comparisons.

More C Programming questions

Exams that ask this topic