package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**260. Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
*/

public class _260 {

    //Approach 1: normal hashmap
    public int[] singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap();
        for (int i : nums) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }

        int[] res = new int[2];
        int index = 0;
        for (int key : map.keySet()) {
            if (map.get(key) == 1) {
                res[index++] = key;
            }
            if (index == 2) {
                break;
            }
        }
        return res;
    }
//    TODO: study its bit manipulation way, this is a MUST!
}

@@ -81,16 +81,20 @@
 ap;%0A%0A/**
+%0A * 
 260. Sin
@@ -108,16 +108,22 @@
 ber III%0A
+ *%0A * 
 Given an
@@ -145,16 +145,19 @@
 rs nums,
+%0A *
  in whic
@@ -244,16 +244,19 @@
 y twice.
+%0A *
  Find th
@@ -604,37 +604,44 @@
     
-//Approach 1: normal hashmap%0A
+public static class Solution1 %7B%0A    
     
@@ -688,16 +688,20 @@
         
+   
+ 
 Map%3CInte
@@ -735,16 +735,20 @@
 hMap();%0A
+    
         
@@ -780,16 +780,20 @@
         
+    
 map.put(
@@ -836,11 +836,19 @@
     
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     
@@ -883,16 +883,20 @@
         
+   
+ 
 int inde
@@ -902,16 +902,20 @@
 ex = 0;%0A
+    
         
@@ -941,24 +941,28 @@
 keySet()) %7B%0A
+    
             
@@ -1002,16 +1002,20 @@
         
+    
 res%5Binde
@@ -1030,34 +1030,40 @@
 ey;%0A            
-%7D%0A
+    %7D%0A  
             if (
@@ -1054,24 +1054,26 @@
             
+  
 if (index ==
@@ -1098,15 +1098,23 @@
     
+    
 break;%0A
+    
     
@@ -1123,34 +1123,42 @@
       %7D%0A        
-%7D%0A
+    %7D%0A    
         return r
@@ -1169,68 +1169,1180 @@
     
-%7D%0A//    TODO: study its bit manipulation way, this is a MUST!
+    %7D%0A    %7D%0A%0A    public static class Solution2 %7B%0A        /**Credit: https://discuss.leetcode.com/topic/21605/accepted-c-java-o-n-time-o-1-space-easy-solution-with-detail-explanations/2%0A         *%0A         * some more explanation about this algorithm:%0A         * two's complement: one number's two's complement number is computed as below:%0A         * reverse all bits of this number and then add one:%0A         * e.g. decimal number 2, in binary format: 0010 (4 bits)%0A         * reversing every single bit becomes 1101,%0A         * then add 1 to it, it becomes 1110%0A         *%0A         * so%0A         * num &= -num, in this case, 2 &= -2 becomes 2%0A         * */%0A        public int%5B%5D singleNumber(int%5B%5D nums) %7B%0A            int diff = 0;%0A            for (int num : nums) %7B%0A                diff %5E= num;%0A            %7D%0A%0A            //get least significant set bit%0A            diff &= -diff;%0A%0A            int%5B%5D result = new int%5B2%5D;%0A            for (int num : nums) %7B%0A                if ((num & diff) == 0) %7B%0A                    result%5B0%5D %5E= num;%0A                %7D else %7B%0A                    result%5B1%5D %5E= num;%0A                %7D%0A            %7D%0A            return result;%0A        %7D%0A    %7D
 %0A%7D%0A

refactor 260