Problem · Array

Single Element in a Sorted Array

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem statement

A sorted array contains one value exactly once and every other value exactly twice. Return the single value in logarithmic time.

Function

singleNonDuplicate(nums: int[]) → int

Examples

Example 1

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

Every value except 2 occupies one adjacent pair.

Constraints

  • 1 <= nums.length <= 100000 and the length is odd.
  • The array is sorted in non-decreasing order.
  • Values fit in a signed 32-bit integer.

More Atlassian problems

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