Curated · Asymptotic Analysis & Recurrences

MCQ · 1 mark · Problem PROG-DIVC-0199

Consider an array of n integer with all numbers are repeated once except one number. Example: array of size 7 as {1,5,10,5,2,1,10}, here that specific number is 2 which is not repeated. What is the time complexity to find that number from array with constant space complexity?
  1. A
    \(O(n)\)
  2. B
    \(O(1)\)
  3. C
    \(O(n^2)\)
  4. D
    \(O(\log n)\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionXOR all the elements: \(x \oplus x = 0\) and \(0 \oplus y = y\), so every number that occurs twice cancels and only the unrepeated number is left.Example: \(1\oplus5\oplus10\oplus5\oplus2\oplus1\oplus10 = 2\).This is one pass over the array with a single accumulator: \(O(n)\) time, \(O(1)\) space.Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com