package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**Given two arrays, write a function to compute their intersection.

 Example:
 Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

 Note:
 Each element in the result should appear as many times as it shows in both arrays.
 The result can be in any order.
 Follow up:
 What if the given array is already sorted? How would you optimize your algorithm?
 What if nums1's size is small compared to nums2's size? Which algorithm is better?
 What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
 Show Tags
 Show Similar Problems
 */
public class _350 {

    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map1 = new HashMap();
        for (int i : nums1) {
            if (map1.containsKey(i)) {
                map1.put(i, map1.get(i) + 1);
            } else {
                map1.put(i, 1);
            }
        }

        List<Integer> list = new ArrayList();
        for (int i : nums2) {
            if (map1.containsKey(i) && map1.get(i) > 0) {
                list.add(i);
                map1.put(i, map1.get(i) - 1);
            }
        }

        int[] result = new int[list.size()];
        int i = 0;
        for (int num : list) {
            result[i++] = num;
        }

        return result;
    }

}

@@ -131,16 +131,61 @@
 Map;%0A/**
+%0A * 350. Intersection of Two Arrays II%0A *%0A * 
 Given tw
@@ -438,16 +438,17 @@
  order.%0A
+%0A
  Follow 
@@ -760,66 +760,72 @@
 e?%0A 
-Show Tags%0A Show Similar Problems%0A */%0Apublic class _350 %7B%0A%0A
+*/%0Apublic class _350 %7B%0A%0A    public static class Solution1 %7B%0A    
     
@@ -883,16 +883,20 @@
         
+    
 Map%3CInte
@@ -912,17 +912,16 @@
 ger%3E map
-1
  = new H
@@ -930,32 +930,36 @@
 hMap();%0A        
+ 
+   
 for (int i : num
@@ -968,47 +968,8 @@
 ) %7B%0A
-            if (map1.containsKey(i)) %7B%0A
     
@@ -975,33 +975,32 @@
              map
-1
 .put(i, map1.get
@@ -998,71 +998,29 @@
  map
-1
 .get
-(i) + 1);%0A            %7D else %7B%0A                map1.put(i,
+OrDefault(i, 0) +
  1);
@@ -1030,35 +1030,29 @@
           %7D%0A
-    
+%0A
     
-%7D%0A%0A
         List
@@ -1085,32 +1085,36 @@
 List();%0A        
+ 
+   
 for (int i : num
@@ -1135,16 +1135,19 @@
     
+   
+ 
 if (map
-1
 .con
@@ -1160,25 +1160,24 @@
 ey(i) && map
-1
 .get(i) %3E 0)
@@ -1195,16 +1195,20 @@
         
+  
+  
 list.add
@@ -1212,16 +1212,20 @@
 add(i);%0A
+    
         
@@ -1235,17 +1235,16 @@
      map
-1
 .put(i, 
@@ -1246,17 +1246,16 @@
 t(i, map
-1
 .get(i) 
@@ -1274,22 +1274,15 @@
     
-  %7D%0A
       
-  %7D%0A
+%7D
 %0A   
@@ -1290,165 +1290,89 @@
     
-int%5B%5D result = new int%5Blist.size()%5D;%0A        int i = 0;%0A        for (int num : list) %7B%0A            result%5Bi++%5D = num;%0A        %7D%0A%0A        return result;
+    %7D%0A            return list.stream().mapToInt(i -%3E i).toArray();%0A        %7D
 %0A    %7D%0A
-%0A
 %7D%0A

refactor 350