✅JAVA || C++ || PYTHON || 🚀log n || 💯Beginners can Understand ||🔥Easy & Intuitive
Upvote if you like the Soltution and Explanation :)\n\n# Intuition \n- Element in pairs, even index, odd index, so find mid and check if it\'s at correct positi
**Upvote if you like the Soltution and Explanation :)**\n\n# Intuition \n- Element in pairs, even index, odd index, so find mid and check if it\'s at correct position or not.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- I already handled the corner cases separately.\n\n- if nums[mid] is same as previous and it is at odd position then it\'s at correct position and if nums[mid] is same as it\'s next and it is at even position then it\'s at correct position so there must be our answer is in right of the mid otherwise it must be in left.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n# Code JAVA\n```\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        int n = nums.length;\n        int l = 0;\n        int r = n-1;\n        \n        if(n==1)\n            return nums[0];\n        if(nums[0] != nums[1])\n            return nums[0];\n        if(nums[n-1] != nums[n-2])\n            return nums[n-1];\n        \n        while(l<=r){\n            int mid = l+(r-l)/2;\n            \n            if(mid>0 && mid < n-1){\n                if(nums[mid] > nums[mid-1] && nums[mid] < nums[mid+1])\n                    return nums[mid];\n                else if((nums[mid] == nums[mid-1] && mid%2==1) || (nums[mid] == nums[mid+1] && mid%2==0))\n                    l = mid+1;\n                else \n                    r = mid -1;\n            }\n        }\n        return 0;\n    }\n}\n```\n\n![image](https://assets.leetcode.com/users/images/dc898633-03c1-4fc3-a7c9-b3dd7be2ec32_1676944210.6929636.jpeg)\n# Code PYHTON 3\n```\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        n = len(nums)\n        l = 0\n        r = n - 1\n        \n        if n == 1:\n            return nums[0]\n        if nums[0] != nums[1]:\n            return nums[0]\n        if nums[n - 1] != nums[n - 2]:\n            return nums[n - 1]\n        \n        while l <= r:\n            mid = l + (r - l) // 2\n            \n            if mid > 0 and mid < n - 1:\n                if nums[mid] > nums[mid - 1] and nums[mid] < nums[mid + 1]:\n                    return nums[mid]\n                elif (nums[mid] == nums[mid - 1] and mid % 2 == 1) or (nums[mid] == nums[mid + 1] and mid % 2 == 0):\n                    l = mid + 1\n                else:\n                    r = mid - 1\n                    \n        return 0\n\n```\n\n# Code C++\n```\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        int n = nums.size();\n        int l = 0;\n        int r = n - 1;\n        \n        if (n == 1)\n            return nums[0];\n        if (nums[0] != nums[1])\n            return nums[0];\n        if (nums[n - 1] != nums[n - 2])\n            return nums[n - 1];\n        \n        while (l <= r) {\n            int mid = l + (r - l) / 2;\n            \n            if (mid > 0 && mid < n - 1) {\n                if (nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1])\n                    return nums[mid];\n                else if ((nums[mid] == nums[mid - 1] && mid % 2 == 1) || (nums[mid] == nums[mid + 1] && mid % 2 == 0))\n                    l = mid + 1;\n                else\n                    r = mid - 1;\n            }\n        }\n        \n        return 0;\n    }\n};\n```\n
['Binary Search', 'C', 'Binary Tree', 'Python', 'Java']