🅰️ Java | easy explained | two approach
\n# Approach 1\n Describe your approach to solving the problem. \nSo, Here we go what about the problem think carefully The first solution for this problem that
\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\nSo, Here we go what about the problem think carefully The first solution for this problem that get accepted. what i did i just run a for loop with starting index of 1 and check for its previous one and with incremental value is +2 here we go. \n1. if it get match then we move ahead other wise it is our element that is single. \n2. consider one of the edge case that is last index what if its last index is that element that nothing to worry about if we did no found any element during this loop that simple mean it is last element and we will return the last element. \n\n# Code in Java \n```\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n       \n       for(int i=1; i<nums.length; i+=2){\n           if(nums[i]!=nums[i-1]){\n               return nums[i-1];\n           }\n       }\n       return nums[nums.length-1];\n    }\n}\n```\nso if you can see in this code that exactly what it did. \n\n# About it complexity :- sorry to say but it is not required complexity even if it get accepted so what? \n- Time complexity: $$O(n)$$\n\n# Approach 2\n Here All we need to modify the solution and as given in question it is sorted array and what it mean the simple strike that comes in mind is ```binary search``` all we need to apply binary search so lets discuss the another problem with like how I can apply ? \nyus exactly that the way like in normal binary search for deciding the left and right pointer we have condition to check what here there is nothing like that. \uD83D\uDE12\n\nAlright after thinking and analyzing some of the patterns here simple look. \n1. there are some basic pattern like first number of pair is present in even index and the another its partner present on the right mean odd index so \n2. suppose if there is no single element thsi pattern will always follow till the end. \n3. but here is a catch this pattern will get disturbed when single element comes into the picture. \n4. so at that exact time we will get to know that yeah there is pattern mismatch and rather then setting left we will set right to mid. \n\n# More Explain \n- Alright here we go for little more explanation \n- Here is input nums = ``[1,1,2,3,3,4,4,8,8]``\n- look closely every first element is on even index and another one is on odd position \n\n![mac-looking.gif](https://assets.leetcode.com/users/images/1b0f0939-dbe6-4ab7-b5a2-bb6fce66dbb5_1676956185.3208497.gif)\n\n- So here is approach if any how we will encounter an element of mid and if it is and even index then it have next same in odd mean next place (mid+1) odd side\n- and if we encounter any odd mid element its partner will present on mid-1;(even side)\n- and if in any case these conditions are not matching what it mean simply that mean pattern get disturbed in between and then we have to set the right to middle \n\n# Code\n```\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n       \n       int l = 0; int r=nums.length-1;\n\n       while(l<r){\n           // simple check for the condition\n           int mid = (r+l)/2;\n\n            if(mid%2==0 && nums[mid+1]==nums[mid]|| mid%2!=0 && nums[mid-1]==nums[mid]){\n                l = mid+1;\n            }else{\n                r = mid;\n            }\n\n       }\n\n       return nums[l];\n\n    }\n}\n```\n\n# Complexity\n- Time complexity:$$O(logn)$$\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# :> Thanks for visiting my Solution. \n
['Java']