Simple C++ code || Approach discussed || TC : O(logN) & SC : O(1) || Binary search
\n\n\n\n\n// Please upvote if it helps :)\n\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        int e = nums.size();\n        /
![image](https://assets.leetcode.com/users/images/3fd1b723-c888-4af7-9e8e-8d09f071871e_1658591320.434945.jpeg)\n\n\n```\n\n// Please upvote if it helps :)\n\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        int e = nums.size();\n        // Checking the boundary first\n        if(e == 1){\n            return nums[0];\n        }\n        \n        if(nums[0] != nums[1]){\n            return nums[0];\n        }\n        \n        if(nums[e-1] != nums[e-2]){\n            return nums[e-1];\n        }\n        \n        int s = 0;\n        int mid = s + (e - s) / 2;\n        int ans = -1;\n        \n        while(s <= e){\n            if(nums[mid] != nums[mid-1] && nums[mid] != nums[mid+1]){\n                ans = nums[mid];\n                break;\n            }\n            // check if it is left half or right half\n            else if((nums[mid] == nums[mid+1] && mid % 2 == 0) || (mid % 2 == 1 && nums[mid] == nums[mid-1])){\n                s = mid + 1;\n            }\n            else{\n                e = mid - 1;\n            }\n            \n            mid = s + (e - s) / 2;\n        }\n        \n        return ans;\n    }\n};\n\n// Please upvote if it helps :)\n\n```
['Binary Search', 'C', 'Python', 'C++']