You Will Never Forget this Approach || Handwritten Dry Run
Please Upvote,it helps a lot to write Such Posts\n\n\n//////////////////////////////////////////////////////////////////////////////////////////////////////////
**Please Upvote,it helps a lot to write Such Posts**\n\n![image](https://assets.leetcode.com/users/images/67a81016-0023-44fd-bbc9-abdd4cec9d79_1637416896.3717594.jpeg)\n/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n![image](https://assets.leetcode.com/users/images/f58a0946-72bb-44ac-872d-c956857ba484_1637416906.2029173.jpeg)\n\n```\n/*Time Complexity : O(log n)\nSpace complexity : O(1)\n\nExplanation: We know that  log n time Complexity can be Achieved by B.Search\nonly and sorted array is given so definitely we can try B.Search. But only one\nissue is there how to determine where to move from mid, so only solution is to\ncheck the number of elements from 0 to mid, so that we can determine on \nwhich side violation is there, then  only we can move when we find on which\nside violation is there and it can be found out only using the odd/even number of elements and comparing the\nmid with mid -1 and mid + 1 to \ndetermince which side we have to go */\n\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        \n        int low = 0,high = nums.length - 1, mid = 0;\n        \n        while(low <= high){\n             mid = (low + high )/2;\n            \n            int num = mid + 1;  //to find total number of elements from 0 to mid\n            \n            if(num % 2 == 0){ //even number of elements between low and high\n                if(mid > 0 && nums[mid] == nums[mid - 1]) low = mid + 1;\n                else if(mid < nums.length - 1 && nums[mid] == nums[mid + 1]) high = mid - 1;\n                else return nums[mid];  \n            }else{\n                 if(mid > 0 && nums[mid] == nums[mid - 1]) high = mid - 1;\n                 else if(mid < nums.length - 1 && nums [mid] == nums[mid + 1]) low = mid + 1;\n                 else return nums[mid];\n        }\n     }\n        return nums[mid];\n    }\n}\n```
['Binary Search', 'Binary Tree', 'Java']