Single Element in a Sorted Array

MediumBinary Searchbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a **sorted** integer array nums in which every value appears exactly twice, except for one value that appears exactly once. Return the value that appears once. Your solution must run in O(log n) time and O(1) extra space, so a linear scan or XOR pass does not meet the bar.

Examples

Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2
2 is the only value without a partner.
Input: nums = [3,3,7,7,10,11,11]
Output: 10
10 appears once; the others appear twice.

Constraints

  • 1 <= nums.length <= 10^5
  • nums.length is odd
  • -10^5 <= nums[i] <= 10^5
  • nums is sorted in non-decreasing order

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems