LeetCodee

540. Single Element in a Sorted Array

Jump to Solution: Python Java C++ JavaScript C#

Problem Description

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

Return the single element that appears only once.

Your solution must run in O(log n) time and O(1) space.

Example 1:

Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2

Example 2:

Input: nums = [3,3,7,7,10,11,11]
Output: 10

Constraints:

  • 1 <= nums.length <= 10⁵
  • 0 <= nums[i] <= 10⁵
  • Every element in nums appears twice except for one element which appears once.

Solution

Python Solution

class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        
        while left < right:
            mid = left + (right - left) // 2
            
            # Ensure mid is even
            if mid % 2 == 1:
                mid -= 1
                
            # If pairs are aligned properly
            if nums[mid] == nums[mid + 1]:
                left = mid + 2
            else:
                right = mid
                
        return nums[left]

Time Complexity: O(log n)

Using binary search to find the single element.

Space Complexity: O(1)

Only using a constant amount of extra space.

Java Solution

class Solution {
    public int singleNonDuplicate(int[] nums) {
        int left = 0, right = nums.length - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            // Ensure mid is even
            if (mid % 2 == 1) {
                mid--;
            }
            
            // If pairs are aligned properly
            if (nums[mid] == nums[mid + 1]) {
                left = mid + 2;
            } else {
                right = mid;
            }
        }
        
        return nums[left];
    }
}

Time Complexity: O(log n)

Using binary search to find the single element.

Space Complexity: O(1)

Only using a constant amount of extra space.

C++ Solution

class Solution {
public:
    int singleNonDuplicate(vector& nums) {
        int left = 0, right = nums.size() - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            // Ensure mid is even
            if (mid % 2 == 1) {
                mid--;
            }
            
            // If pairs are aligned properly
            if (nums[mid] == nums[mid + 1]) {
                left = mid + 2;
            } else {
                right = mid;
            }
        }
        
        return nums[left];
    }
};

Time Complexity: O(log n)

Using binary search to find the single element.

Space Complexity: O(1)

Only using a constant amount of extra space.

JavaScript Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNonDuplicate = function(nums) {
    let left = 0, right = nums.length - 1;
    
    while (left < right) {
        let mid = left + Math.floor((right - left) / 2);
        
        // Ensure mid is even
        if (mid % 2 === 1) {
            mid--;
        }
        
        // If pairs are aligned properly
        if (nums[mid] === nums[mid + 1]) {
            left = mid + 2;
        } else {
            right = mid;
        }
    }
    
    return nums[left];
};

Time Complexity: O(log n)

Using binary search to find the single element.

Space Complexity: O(1)

Only using a constant amount of extra space.

C# Solution

public class Solution {
    public int SingleNonDuplicate(int[] nums) {
        int left = 0, right = nums.Length - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            // Ensure mid is even
            if (mid % 2 == 1) {
                mid--;
            }
            
            // If pairs are aligned properly
            if (nums[mid] == nums[mid + 1]) {
                left = mid + 2;
            } else {
                right = mid;
            }
        }
        
        return nums[left];
    }
}

Time Complexity: O(log n)

Using binary search to find the single element.

Space Complexity: O(1)

Only using a constant amount of extra space.

Approach Explanation

The solution uses binary search with pattern observation:

  1. Key Insights:
    • Array pattern
    • Binary search
    • Even-odd indices
    • Pattern disruption
  2. Algorithm Steps:
    • Initialize pointers
    • Find mid point
    • Check pattern
    • Update bounds

Implementation Details:

  • Binary search
  • Index manipulation
  • Pattern checking
  • Boundary updates

Optimization Insights:

  • Logarithmic time
  • Constant space
  • Early termination
  • Efficient comparison

Edge Cases:

  • Single element
  • Element at start
  • Element at end
  • Middle element