Just one pass! hashset is enough!


  • -11
    N
      public static int[] singleNumber(int[] nums) {
        int[] list=new int[2]; 
        HashSet<Integer> rs=new HashSet<Integer>();
        for(int i=0;i<nums.length;i++)
        {
            if(!rs.add(nums[i]))  rs.remove(nums[i]);
        }
        Iterator<Integer> iterator=rs.iterator();
        int i=0;
    	while(iterator.hasNext()){
    		list[i++]=iterator.next();
    	}
        return list;
    
    }

  • -1
    O

    you can simply return rs.ToArray()


Log in to reply
 

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