Fundamental Binary Search Pattern
\n# Code\njava []\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n\n        int n = nums.length;\n\n        if (n==1) return nums[0];\n     
\n# Code\n```java []\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n\n        int n = nums.length;\n\n        if (n==1) return nums[0];\n        if(nums[0]!=nums[1]) return nums[0];\n        if(nums[n-1]!=nums[n-2]) return nums[n-1];\n\n        int low =1;\n        int high = n-2;\n        while (low<=high){\n\n            int mid = (low+high)/2;\n\n            if(nums[mid]!=nums[mid-1] && nums[mid]!=nums[mid+1]) return nums[mid];\n\n            else if (\n                (mid%2==1 && nums[mid]== nums[mid-1]) ||\n                (mid%2==0 && nums[mid]== nums[mid+1])\n            )\n            {\n                low = mid+1;\n            }\n            else {\n                high = mid-1;\n            }\n        }\n        return -1;\n        \n    }\n}\n```
['Java']