C++ | Clean Code (5 Lines) | Easy to understand | XOR Method | Well explained
Pls upvote the thread if you found it helpful.\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n    We know :-\n        - XOR(x,x) 
**Pls upvote the thread if you found it helpful.**\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n    We know :-\n        - XOR(x,x) = 0\n        - XOR(x,0) = x\nUsing this the **repeating elements** will become 0 and the element that is **present once** will be left.\nIterate over the given sorted array and maintain a curr_xor which will store XOR of the elements.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n    1. Initialize variables. \n    curr_xor = 0 and n (size of the given sorted array)\n    2. Iterate over the given array \n    3. XOR elements.\n    curr_xor ^= nums[i]\n    4. Return curr_xor\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)    \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n# Code\n```\nclass Solution {\npublic:\n    int singleNonDuplicate(vector<int>& nums) {\n        int n = nums.size();\n        int curr_xor=0; //variable will store XOR\n        for(int i =0;i<n;i++){\n            curr_xor ^= nums[i]; // XOR\n        }\n        return curr_xor;\n    }\n};\n```
['Array', 'Math', 'Binary Search', 'Bit Manipulation', 'C++']