@@ -0,0 +1,1331 @@
+package array.lc080_removeduplicatesfromsortedarray2;%0A%0A/**%0A * Follow up for %22Remove Duplicates%22: What if duplicates are allowed at most twice?%0A * For example, Given sorted array nums = %5B1,1,1,2,2,3%5D.%0A * Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3.%0A * It doesn't matter what you leave beyond the new length.%0A */%0Apublic class Solution %7B%0A    public int removeDuplicates(int%5B%5D nums) %7B%0A        if (nums.length %3C= 2) %7B%0A            return nums.length;%0A        %7D%0A%0A        // Invariant: j points to position ready to insert%0A        //  property-1: %5B0,j) elements appear no more than twice%0A        //  property-2: nums%5Bj%5D == nums%5Bi%5D ?%0A        // Thus%0A        //  to maintain 2 =%3E must move forward j if nums%5Bi%5D is different (e.g. 1,1,1(j),1,1,2(i))%0A        //  to maintain 1 =%3E must copy nums%5Bi%5D to nums%5Bj%5D before move j%0A        //  when loop terminates, %5B0,j) is what we want%0A        int j = 1;%0A        int c = 1;%0A        for (int i = 1; i %3C nums.length; i++) %7B%0A            if (nums%5Bi%5D == nums%5Bi-1%5D) %7B%0A                if (c %3C 2) %7B%0A                    nums%5Bj%5D = nums%5Bi%5D;%0A                    j++;%0A                %7D%0A                c++;%0A            %7D else %7B%0A                nums%5Bj%5D = nums%5Bi%5D;%0A                j++;%0A                c = 1;%0A            %7D%0A        %7D%0A        return j;%0A    %7D%0A%7D%0A

leetcode-80 remove duplicates from sorted array 2