Very easy solution in C++ || Basic approach (beats 100%)
iam explained indetail in my website and youtube vedio if you have more clearity on this problem and approachs visite the below website and youtube channel:\nWE
iam explained indetail in my website and youtube vedio if you have more clearity on this problem and approachs visite the below website and youtube channel:\nWEBSITE:\nhttps://vinayhac.blogspot.com/2023/06/540.%20Single%20Element%20in%20a%20Sorted%20Array.html\nYOUTUBE VEDIO:\nhttps://youtu.be/vGtAZA62xC8\n\nin this problem we solve this using 3 approachs \n1. bitwise operations\n2. unodered_map algorithm\n3. general basic concepts \n**APPROACH- 1:**\nlets start with the first bitwise opertaion this problem we solve applying the xor to all elements then it find the single element in the entire array\n**CODE:**\n```\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        // bit \n        // unordered map \n        // generally basics \n        \n        /* \n        xor operation (^)\n        1-> 8421= 0001\n        3-> 8421= 0011\n        --------------\n        output:\n                  0010 (2)\n                  \n       1-> 8421= 0001  3-> 8421 =0011\n       1-> 8421= 0001  3-> 8421 =0011\n       \n                   0000(0)*/\n        int s=0;\n        int i;//index\n        for(i=0;i<nums.size();i++){\n           s=s^nums[i]; \n        }\n        return s;\n       \n    }\n};\n```\n**APPROACH-2:**\nin this we use the unodered map algorithm here finding the each element frequency the unordered_map help alot to solve this problems and based on the problem the use will be changed\n**CODE:**\n```\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        // bit \n        // unordered map \n        // generally basics \n        \n        /* \n        unordered_map<int,int>mp;\n                      key value*/\n        unordered_map<int,int>map;\n        int i;//index\n        int n=nums.size();//size\n        for(i=0;i<n;i++){\n            map[nums[i]]++;\n        }\n        for(auto l:map){\n            if(l.second==1){\n                return l.first;\n            }\n        }\n       return 0; \n       \n    }\n};\n```\n**APPROACH-3:**\nin this method in problem statement only given the array is the sorted array and each integer appers twice but on e integer appers only once we travelling in even index format to find the single apper integer\n**CODE:**\n```\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        // bit \n        // unordered map \n        // generally basics \n        \n        /* \n         even index \n         0 1 2 3 4 5 6\n         1 1 2 3 3 4 4\n         \n         \n         */\n        if(nums.size()==1){\n            return nums[0];\n        }\n        int i;//index\n        for(i=0;i<nums.size();i+=2){\n            if(nums[i]!=nums[i+1]){\n                return nums[i];\n            }\n        }\n        return 0;\n       \n    }\n};\n```\n
['Math']