1. /**
    2. * leetcode#540 有序数组中的单一元素
    3. * @desc 给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。
    4. * 输入: [1,1,2,3,3,4,4,8,8]
    5. * 输出: 2
    6. * 输入: [3,3,7,10,10,11,11]
    7. * 输出: 10
    8. * **/
    9. function singleNonDuplicate(nums) {
    10. let l = 0; h = nums.length - 1;
    11. while (l < h) {
    12. let m = Math.floor((l + h) / 2);
    13. // 确保m是偶数
    14. if (m % 2 == 1) {
    15. m -= 1;
    16. }
    17. if (nums[m] == nums[m + 1]) {
    18. l = m + 2
    19. } else {
    20. h = m
    21. }
    22. }
    23. return nums[l]
    24. };
    25. console.log(singleNonDuplicate([1, 1, 2, 3, 3, 4, 4, 8, 8]))