Simple Solution with Diagrams in Video - JavaScript, C++, Java, Python
Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu
# Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nThanks!\n\nhttps://youtu.be/WHC5muNp2NQ?si=VHyNJgx7J1kZXjD_\n\n\n```Javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar singleNonDuplicate = function (nums) {\n    // initilaize the left and right pointer\n    let l = 0,\n        r = nums.length - 1;\n\n    while (l != r) {\n\n        // set the value of mid\n        let mid = l + Math.floor((r - l) / 2);\n\n        // if mid is odd, decrement it to make it even\n        if (mid % 2 == 1) {\n            mid--;\n        }\n        // if the elements at mid and mid + 1 are the same, then the single element must appear after the midpoint\n        if (nums[mid] == nums[mid + 1]) {\n            l = mid + 2;\n        }\n\n        else {\n            // otherwise, we must search for the single element before the midpoint\n            r = mid;\n        }\n    }\n    return nums[l];\n};\n```\n```Python []\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        # Initialize the left and right pointers\n        l, r = 0, len(nums) - 1\n\n        while l != r:\n            # Set the value of mid\n            mid = l + (r - l) // 2\n\n            # If mid is odd, decrement it to make it even\n            if mid % 2 == 1:\n                mid -= 1\n\n            # If the elements at mid and mid + 1 are the same,\n            # then the single element must appear after the midpoint\n            if nums[mid] == nums[mid + 1]:\n                l = mid + 2\n            # Otherwise, we must search for the single element\n            # before the midpoint\n            else:\n                r = mid\n\n        return nums[l]\n\n```\n```Java []\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        // initilaize the left and right pointer\n        int l = 0;\n        int r = nums.length - 1;\n\n        while (l < r) {\n\n            // if mid is odd, decrement it to make it even\n            int mid = l + (r - l) / 2;\n            if (mid % 2 == 1)\n                mid--;\n\n            // if the elements at mid and mid + 1 are the same, \n            // then the single element must\n            // appear after the midpoint\n            if (nums[mid] == nums[mid + 1]) {\n                l = mid + 2;\n            }\n            // otherwise, we must search for the \n            // single element before the midpoint\n            else {\n                r = mid;\n            }\n        }\n        return nums[l];\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        // Initialize the left and right pointers\n        int l = 0;\n        int r = nums.size() - 1;\n\n        while (l < r) {\n            // Set the value of mid\n            int mid = l + (r - l) / 2;\n\n            // If mid is odd, decrement it to make it even\n            if (mid % 2 == 1) {\n                mid--;\n            }\n\n            // If the elements at mid and mid + 1 are the same,\n            // then the single element must appear after the midpoint\n            if (nums[mid] == nums[mid + 1]) {\n                l = mid + 2;\n            }\n            // Otherwise, we must search for the single element before the\n            // midpoint\n            else {\n                r = mid;\n            }\n        }\n\n        return nums[l];\n    }\n};\n\n```\n
['Python', 'C++', 'Java', 'Python3', 'JavaScript']