[JAVA] Binary Search || 100% fast || Each step explained with comments
\'\'\'\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        \n        // As we are given a sorted array + it says that our solution must 
\'\'\'\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        \n        // As we are given a sorted array + it says that our solution must be of 0(log n) time\n        // We, get the idea that we have to do BINARY SEARCH\n        \n        //BOUNDARY CONDITIONS:\n        \n        //1. Check if the length of nums is only 1     \n        \n        if(nums.length == 1)\n        {\n            return nums[0];\n        }\n        \n        //2. Check if the first element is our answer?\n        \n        else if(nums[0] != nums[1])\n        {\n            return nums[0];\n        }\n        \n        //3. Check if the last element of the array is our answer?\n        \n        else if(nums[nums.length-1] != nums[nums.length-2])\n        {\n            return nums[nums.length-1];\n        }\n        \n        int start = 0;\n        int end = nums.length-1;\n        \n        while(start <= end)\n        {\n            int mid = start + (end - start)/2;\n            \n\t\t\t// Here, we are checking that if the value at mid is not equal to any of its adjacent then that is our answer\n            \n            if((nums[mid] != nums[mid+1]) && (nums[mid] != nums[mid-1]))\n            {\n                return nums[mid];\n            }  \n            \n\t\t\t//Here for odd positions, we are checking the condition with mid-1, as the pair would definitely be on its left\n\t\t\t//And for the even position we have mid+1 as the pair would be on the right\n            \n            else if(((mid % 2 == 0) && (nums[mid] == nums[mid+1])) || ((mid % 2 == 1) && (nums[mid] == nums[mid-1])))\n            {\n                start = mid + 1;\n            }\n            else\n            {\n                end = mid - 1;\n            }             \n        }\n        \n        return -1;\n    }\n}\n\'\'\'
['Binary Search', 'Binary Tree', 'Java']