HashMap || Bitwise XOR ||Easy to understand || Java 2 Solutions
Approach\n Describe your approach to solving the problem. \nApproach 1: Bit manupulation - TC O(n) - SC (1)\nHere we use the fact that performing XOR with two s
# Approach\n<!-- Describe your approach to solving the problem. -->\n**Approach 1**: Bit manupulation - TC O(n) - SC (1)\nHere we use the fact that performing XOR with two same numbers give us zero and doing XOR with zero and other number will give us that number itself.\nhere\'s the example\n2 - 0010\n2 - 0010\n3 - 0011\n\nSo 2^2^3 = 3\n\nSo we simply iterate through the loop and do XOR and the final ans will be the required ans\n**Approach 2**:HashMap\nWe use an HashMap to make a frequency table to note number of occurrences of the elements.\nThen we use an Entry Set to iterate and return the element with the single occurrence.\n\n\n# Code\n```\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        int ans=0;\n        for(int i=0;i<nums.length;i++){\n            ans^=nums[i];\n        }\n    return ans;\n    }\n}\n```\n```\nclass Solution {\n    public int singleNonDuplicate(int[] nums) {\n        HashMap<Integer,Integer> hs= new HashMap<>();\n        for(int i:nums)\n        {\n            hs.put(i,hs.getOrDefault(i,0)+1);\n        }\n        System.out.println(hs);\n        for(Map.Entry<Integer,Integer> mp:hs.entrySet())\n        {\n            if(mp.getValue()==1)\n            return mp.getKey();\n        }\n    return -1;\n    }\n}\n```
['Java']