Used two hashset in Java---single number II


  • -1
    X

    public class Solution {

      public int singleNumber(int[] nums) {
       Set<Integer> s = new HashSet<Integer>();
    	Set<Integer> s2 = new HashSet<Integer>();
         for(int i=0;i<nums.length;i++)
         {   if(s.contains(nums[i])&&(s2.contains(nums[i])))
            { 
        	 s.remove(nums[i]);
             }else if(s.contains(nums[i])&&(!s2.contains(nums[i]))){
            	 s2.add(nums[i]);
             }
          
          else if(!s.contains(nums[i])&&(!s2.contains(nums[i]))){
        	  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;   
    }
    

    }


Log in to reply
 

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