Hashset in JAVA --single number


  • -1
    X

    public class Solution {

         public int singleNumber(int[] nums) {
    
    	   Set<Integer> s = new HashSet<Integer>();
    	     for(int i=0;i<nums.length;i++)
    	     {   if(s.contains(nums[i]))
    	        { s.remove(nums[i]);
    	         
    	         }
    	      
               else{ s.add(nums[i]); } 
    	        
    	         
    	         
    	     }
    	     int aa=0;
    	     for (int i = 0; i < nums.length; i++){
    	         if(s.contains(nums[i])) { aa=nums[i];}
    	     } 
    	     return aa;
     
    }
    

    }


  • 2

    Your solution doesn't comply to :Could you implement it without using extra memory?


  • 0
    D

    So, if we have to do this in place, XOR is the only way


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.