Problem · Array

Single Element in a Sorted Array

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEONSITE INTERVIEW

Problem statement

Given a sorted integer array nums, 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 use O(1) extra space.

Function

singleNonDuplicate(nums: int[]) → int

Examples

Example 1

nums = [1,1,2,3,3,4,4]return = 2

Every value except 2 belongs to an adjacent pair.

Example 2

nums = [0]return = 0

The only element is the unique value.

Constraints

  • 1 <= nums.length <= 100000
  • nums.length is odd.
  • -2147483648 <= nums[i] <= 2147483647
  • nums is sorted in nondecreasing order.
  • Exactly one value appears once; every other value appears exactly twice.

More Goldman Sachs problems

drafts saved locally
public int singleNonDuplicate(int[] nums) {
    // Write your code here
}
nums[1,1,2,3,3,4,4]
expected2
checking account