package com.fishercoder.solutions;

import java.util.ArrayList;

/**
 * Follow up for "Remove Duplicates":
 What if duplicates are allowed at most twice?

 For example,
 Given sorted array nums = [1,1,1,2,2,3],

 Your function should return length = 5,
 with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
 */
public class _80 {

    public int removeDuplicates(int[] nums) {
        int counter = 0;
        int len = nums.length;
        if (len == 0) {
            return 0;
        }
        if (len == 1) {
            return 1;
        }
        if (len == 2) {
            return 2;
        }

        ArrayList<Integer> a = new ArrayList();
        a.add(nums[0]);
        a.add(nums[1]);
        for (int i = 2; i < len; i++) {
            if (nums[i] != nums[i - 1]) {
                a.add(nums[i]);
            } else if (nums[i] != nums[i - 2]) {
                a.add(nums[i]);
            }
        }

        counter = a.size();
        for (int i = 0; i < counter; i++) {
            nums[i] = a.get(i);
        }

        return counter;
    }

}

@@ -61,11 +61,83 @@
 st;%0A
-%0A/*
+import java.util.List;%0A%0A/**%0A * 80. Remove Duplicates from Sorted Array II%0A 
 *%0A *
@@ -460,16 +460,50 @@
  _80 %7B%0A%0A
+  public static class Solution1 %7B%0A
     publ
@@ -542,26 +542,24 @@
 ms) %7B%0A      
-  
 int counter 
@@ -559,26 +559,24 @@
 ounter = 0;%0A
-  
       int le
@@ -592,18 +592,16 @@
 length;%0A
-  
       if
@@ -618,28 +618,24 @@
 ) %7B%0A        
-    
 return 0;%0A  
@@ -630,38 +630,34 @@
 return 0;%0A      
- 
- 
 %7D%0A
-  
       if (len ==
@@ -666,28 +666,24 @@
 ) %7B%0A        
-    
 return 1;%0A  
@@ -686,26 +686,24 @@
 ;%0A      
-  
 %7D%0A      
   if (le
@@ -690,26 +690,24 @@
     %7D%0A      
- 
- 
 if (len == 2
@@ -714,28 +714,24 @@
 ) %7B%0A        
-    
 return 2;%0A  
@@ -726,34 +726,32 @@
 return 2;%0A      
- 
- 
 %7D%0A%0A        Array
@@ -747,15 +747,8 @@
     
-  Array
 List
@@ -774,26 +774,24 @@
 rrayList();%0A
-  
       a.add(
@@ -802,26 +802,24 @@
 %5B0%5D);%0A      
-  
 a.add(nums%5B1
@@ -818,26 +818,24 @@
 d(nums%5B1%5D);%0A
-  
       for (i
@@ -868,20 +868,16 @@
         
- 
-   
 if (nums
@@ -900,38 +900,32 @@
 1%5D) %7B%0A          
-      
 a.add(nums%5Bi%5D);%0A
@@ -932,20 +932,16 @@
         
-  
-  
 %7D else i
@@ -975,30 +975,24 @@
 %7B%0A          
-      
 a.add(nums%5Bi
@@ -1003,24 +1003,18 @@
         
-  
-  %7D%0A  
+%7D%0A
       %7D%0A
@@ -1010,26 +1010,24 @@
  %7D%0A      %7D%0A%0A
-  
       counte
@@ -1042,26 +1042,24 @@
 ze();%0A      
-  
 for (int i =
@@ -1090,20 +1090,16 @@
         
-   
- 
 nums%5Bi%5D 
@@ -1120,21 +1120,16 @@
     
-  %7D%0A%0A 
+%7D%0A
       
- 
 retu
@@ -1145,12 +1145,16 @@
 r;%0A    %7D
+%0A  %7D
 %0A%0A%7D%0A

refactor 80