Multiple_Approaches.py
UPVOTE IF YOU LIKE SOLN \n\n# Approach\n Brute Force -> Optimization\n\n# Complexity\n- Time complexity:\nO(n) --> O(log n)\n# Codes\n```\n//Brute Force OP\ncla
<h3>UPVOTE IF YOU LIKE SOLN</h3>\n\n# Approach\n<h4>Brute Force -> Optimization\n\n# Complexity\n- Time complexity:\n$$O(n)$$ --> $$O(log n)$$\n# Codes\n```\n//Brute Force OP\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        for(int p=0;p<nums.size()-1;p+=2){\n            if(nums[p]!=nums[p+1]){\n                return nums[p];\n            }\n        }\n        return nums[nums.size()-1];\n    }\n};\n```\n```\n#Using XOR\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        x=0\n        for i in nums:\n            x=x^i\n        return x\n```\n```\n#One-Liner Using Set\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        return ((2*sum(set(nums)))-sum(nums))\n\n```\n```\n#One_liner Xor\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        return reduce(lambda m,n:m^n,nums)\n```\n```\n//Another One\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        map<int,int>mp;\n        int n=nums.size();\n        for(int p=0;p<n;p++) mp[nums[p]]++;\n        for(auto i:mp){\n            if(i.second==1) return i.first;\n        }\n        return 0;\n    }\n};\n```\n```\n//Final One\n//Binary Search Optimised\nclass Solution:\n    def singleNonDuplicate(self, nums: List[int]) -> int:\n        l,r=0,len(nums)//2\n        ans=-1\n        while l<=r:\n            mid=(l+r)//2\n            i=mid*2\n            if i+1>=len(nums) or nums[i]!=nums[i+1]:\n                r=mid-1\n                ans=nums[i]\n            else:\n                l=mid+1\n        return ans\n```\n\n![](https://assets.leetcode.com/users/images/59cc0319-232b-4d39-9d5c-48a5a458b05f_1652453667.350621.jpeg)
['Python3']