↓Skip to main content
  1. LeetCode/

LeetCode 540: Single Element in a Sorted Array

·2 mins· ·
LeetCode Medium Array Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-27

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

Before the single element each pair starts at an even index; after it, at an odd index (Binary Search). Make mid even and check nums[mid] == nums[mid + 1]:

  • Equal: everything before is complete pairs, so the answer is to the right, l = mid + 2.
  • Not equal: the answer is at mid or to its left, r = mid.

My Attempts
#

I originally scanned pairs (O(n), while the problem asks for O(log n)). The first two versions missed a single element at the end: the loop finished without returning, so [7, 7, 8] returned None instead of 8, and a lone [3] did too.

Solution
#

class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        l, r = 0, len(nums) - 1
        while l < r:
            mid = (l + r) // 2
            if mid % 2:
                mid -= 1                                # 調成偶數
            if nums[mid] == nums[mid + 1]:
                l = mid + 2
            else:
                r = mid
        return nums[l]

Complexity
#

Time O(log n), space O(1).

Mistakes to Avoid
#

  • The problem requires O(log n); scanning pairs is O(n).
  • If the single element is last, a pairwise loop never finds it and needs a final return.

Interview Explanation
#

Pairs before the single element start at even indices, so I align mid to even and compare it with the next: equal means the answer is to the right, otherwise at or left of mid. Time O(log n).

Related

LeetCode 275: H-Index II
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 275 note: with sorted citations, find the first i where citations[i] >= n - i; the answer is n - i, in O(log n) with binary search
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 240: Search a 2D Matrix II
·2 mins
LeetCode Medium Array Binary-Search Divide-and-Conquer Matrix
LeetCode 240 note: start at the top-right corner, moving left when larger and down when smaller, eliminating a row or column each step
LeetCode 162: Find Peak Element
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 162 note: nums[mid] < nums[mid + 1] guarantees a peak to the right (rising until the edge at worst), otherwise one at or left of mid
LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 153: Find Minimum in Rotated Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 153 note: compare mid with the right end: nums[mid] > nums[r] puts the minimum to the right, otherwise mid may be it, so r = mid