package com.fishercoder.solutions;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/

public class _136 {

    public static class Solution1 {
        /**approach 1: use set, since this problem explicitly states that every element appears twice and only one appears once
        so, we could safely remove the ones that are already in the set, O(n) time and O(n) space.
        HashTable approach works similarly like this one, but it could be more easily extend to follow-up questions.*/
        public int singleNumber(int[] nums) {
            Set<Integer> set = new HashSet();
            for (int i : nums) {
                if (!set.add(i)) {
                    set.remove(i);
                }
            }
            Iterator<Integer> it = set.iterator();
            return it.next();
        }
    }


    public static class Solution2 {
        /**approach 2: bit manipulation, use exclusive or ^ to solve this problem:
        we're using the trick here: every number ^ itself will become zero, so, the only remaining element will be the one that
        appeared only once.*/
        public int singleNumber(int[] nums) {
            int res = 0;
            for (int i : nums) {
                res ^= i;
            }
            return res;
        }
    }

}

@@ -127,16 +127,17 @@
  Number%0A
+%0A
 Given an
@@ -361,18 +361,16 @@
 _136 %7B%0A%0A
-  
   public
@@ -395,32 +395,36 @@
 tion1 %7B%0A    
-    /**a
+/**%0A     * A
 pproach 1: u
@@ -500,16 +500,23 @@
 rs twice
+%0A     *
  and onl
@@ -533,24 +533,16 @@
 ars once
-%0A       
  so, we 
@@ -598,16 +598,23 @@
 the set,
+%0A     *
  O(n) ti
@@ -631,24 +631,16 @@
 ) space.
-%0A       
  HashTab
@@ -697,16 +697,23 @@
 could be
+%0A     *
  more ea
@@ -747,23 +747,25 @@
 estions.
-*/
 %0A    
+ */%0A
     publ
@@ -794,30 +794,24 @@
 t%5B%5D nums) %7B%0A
-      
       Set%3CIn
@@ -836,38 +836,32 @@
 ashSet();%0A      
-   
-   
 for (int i : num
@@ -873,24 +873,16 @@
         
-    
-    
 if (!set
@@ -902,26 +902,16 @@
         
-  
-        
 set.remo
@@ -929,44 +929,24 @@
     
-     
-   %7D%0A            
 %7D%0A   
+   %7D%0A
       
-   
 Iter
@@ -980,22 +980,16 @@
 ator();%0A
-      
       re
@@ -1008,28 +1008,19 @@
 ();%0A
-    
     %7D%0A
-  
   %7D%0A%0A
-%0A  
   pu
@@ -1057,16 +1057,20 @@
     
-    /**a
+/**%0A     * A
 ppro
@@ -1135,24 +1135,16 @@
 problem:
-%0A       
  we're u
@@ -1157,16 +1157,23 @@
 he trick
+%0A     *
  here: e
@@ -1261,21 +1261,20 @@
  one
- that
 %0A     
-  
+* that
  app
@@ -1289,23 +1289,25 @@
 ly once.
-*/
 %0A    
+ */%0A
     publ
@@ -1346,22 +1346,16 @@
 %7B%0A      
-    
-  
 int res 
@@ -1359,22 +1359,16 @@
 es = 0;%0A
-      
       fo
@@ -1394,24 +1394,16 @@
         
-     
-   
 res %5E= i
@@ -1414,28 +1414,16 @@
     
-      %7D%0A    
+%7D%0A
       
-  
 retu
@@ -1434,23 +1434,16 @@
 es;%0A
-        %7D%0A
     %7D%0A
+  %7D
 %0A%7D%0A

refactor 136