Prepin
Log in

Consider an array of numbers in which each number appears twice, except for one number that appears only once. What approach would you take to identify this unique number in O(log n) time?

Approach / Explanation

This question presents an array of integers in which each number appears twice, except for one number that occurs only once. Your objective is to identify that unique number. Additionally, you are required to accomplish this with a time complexity of O(log n). This means you must devise an efficient algorithm where the execution time grows logarithmically as the number of inputs, n, increases. To successfully tackle this question, you should grasp the following concepts: The idea of time complexity: O(log n) signifies a logarithmic relationship between the number of operations needed and the size of the input. It is characteristic of divide-and-conquer approaches where the problem size is halved (or significantly reduced) with each step, similar to binary search. The data structure involved: An array, which is a linear data structure used for storing elements of the same type. Understanding binary operations like XOR (exclusive OR), which could provide a solution to this problem in linear time. It is important to note that while a linear solution utilizing the XOR operation exists, it does not satisfy the O(log n) time complexity requirement stated in the question. Achieving O(log n) time complexity necessitates a divide-and-conquer approach, which is not possible with the XOR operation.

Suggested Answer

Considering the requirements of the question, we are employing a binary search method to tackle the problem, which adds some complexity. Let's break it down step by step: Initially, sort the array if it hasn't been sorted yet, but keep in mind that sorting will alter the time complexity; therefore, we will assume the array is already sorted in advance. Implement the binary search technique by splitting the array into two halves. Examine the middle index. If the middle index is even and the value at this index matches the value at the next index, it indicates that the single number is located to the right of the middle. This is because all elements to the left of the middle have appeared twice. If not, the single number will be found in the left half of the array. It is important to note that in a sorted array where every number appears twice, even indexes should share the same value as the next index. This principle does not apply when we are at an index corresponding to a single number, which helps to narrow the search area. (Note: Indexing begins at 0)

Alternative Answer

Let's examine a Python example. We have the array [1,2,2,3,3,4,4,5,5] and we need to identify the number that occurs only once: def singleNumber ( arr , low , high ) : if low > high : return None if low == high : return arr [ low ] mid = int ( ( high - low ) / 2 ) + low if mid % 2 == 0 : # The element at mid is the same as the next element if arr [ mid ] == arr [ mid + 1 ] : return singleNumber ( arr , mid + 2 , high ) else : return singleNumber ( arr , low , mid ) else : if arr [ mid ] == arr [ mid - 1 ] : return singleNumber ( arr , mid + 1 , high ) else : return singleNumber ( arr , low , mid - 1 ) # Sample driver code arr = [ 1 , 2 , 2 , 3 , 3 , 4 , 4 , 5 , 5 ] n = len ( arr ) print ( The element that appears once is' , singleNumber ( arr , 0 , n - 1 ) ) The function singleNumber employs a binary search technique and utilizes the concept of even index pairs to locate the single number within the array.

Question Details

Difficulty & Category

Medium
coding

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.