Leetcode Problem 540. Single Element in a Sorted Array

540. Single Element in a Sorted Array

Leetcode Solutions

Binary Search on Even Indexes

  1. Initialize lo to 0 and hi to the last index of the array.
  2. While lo is less than hi: a. Calculate mid as the midpoint of lo and hi. b. If mid is odd, decrement it by 1 to ensure it is even. c. If the element at mid is equal to the element at mid + 1, set lo to mid + 2. d. Otherwise, set hi to mid.
  3. Once lo equals hi, the element at this index is the single element. Return it.
UML Thumbnail

Brute Force Linear Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...