FastPrepSingle Element in a Sorted Array
Problems/Single Element in a Sorted Array

Single Element in a Sorted Array

GreyOrange logoGreyOrange● MediumFULLTIMEPHONE SCREEN

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a GreyOrange SDE II interview, as the second coding question of the first round, a data structures and algorithms round. The report names the task and sums it up in one line: a sorted array in which all values come in pairs except one value, which is the answer.

The report gives no examples or limits. Here we use the usual limits for this task, with values from 0 to 10^5, and ask for a solution in O(log n) time and O(1) extra space. The report describes a binary search over pair positions and says the interviewer gave hints; the editorial goes from a linear pass to that binary search.

Our version is about 95% the same as the reported question.

You are given an integer array nums sorted in non-decreasing order. Every value in nums appears exactly twice, except for one value that appears exactly once.

Return the value that appears exactly once.

Your solution should run in O(log n) time and use O(1) extra space, where n is the length of nums.

Function

singleNonDuplicate(nums: int[]) → int

Examples

Example 1

nums = [1,1,4,4,6,8,8]return = 6

1, 4 and 8 each appear twice. 6 appears once.

Example 2

nums = [3,5,5,9,9]return = 3

The single value can be the first element. 5 and 9 each appear twice after it.

Example 3

nums = [2,2,4,4,7]return = 7

The single value can also be the last element.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5
  • nums is sorted in non-decreasing order.
  • Exactly one value appears once; every other value appears exactly twice.

More GreyOrange interview questions

  • Lowest Common Ancestor in a BST and a Binary TreePHONE SCREEN · Seen Dec 2026
  • Array OperationsOA · Seen Oct 2024
  • Roman Numerals in StringsOA · Seen Oct 2024
  • Triangle and PointsOA · Seen Oct 2024

Similar questions at other companies

  • Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Search in a Rotated Sorted ArrayAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
  • Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Find First and Last Position in a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
  • Longest Increasing SubsequenceAngel One · PHONE SCREEN
  • Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Find First and Last Position of Element in Sorted ArrayEPAM · PHONE SCREENEPAM interview questions
  • Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN
  • Search in Rotated Sorted ArrayZoomInfo · PHONE SCREEN
All GreyOrange interview questionsSee GreyOrange hiring insights
Saved
class Solution {
    public int singleNonDuplicate(int[] nums) {
      // Write your code here.
    }

}
Checking account…