https://leetcode.com/problems/single-element-in-a-sorted-array/description/
class Solution { public: int singleNonDuplicate(vector<int>& nums) { // 1 1 2 // 1 2 2 // 1 1 2 3 3 // 1 2 2 3 3 // 1 1 2 2 3 int l = 0, r = nums.size() - 1; while (l < r) { int m = l + (r - l) / 2; if (nums[m] == nums[m-1]) { if (m % 2) l = m + 1; else r = m - 2; } else if (nums[m] == nums[m+1]) { if (m % 2) r = m - 1; else l = m + 2; } else return nums[m]; } return nums[l]; } /* int singleNonDuplicate(vector<int>& nums) { // 1 1 2 // 3 4 4 // 1 1 2 3 3 // 1 1 2 2 3 // 1 2 2 3 3 int l = 0, r = nums.size() - 1; while (l < r) { int m = l + (r - l) / 2; if (nums[m] == nums[m+1]) { if (m % 2) // left odd r = m - 1; else l = m; } else if (nums[m] == nums[m-1]) { if (m % 2) // left odd l = m + 1; else r = m; } else return nums[m]; } return nums[l]; } */ };