Remove Duplicates From Sorted Array II
Given an integer array numsnums
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part
of the array numskknumsk
Return kknums
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
1 <= nums.length <= 3 * 104-104 <= nums[i] <= 104 is sorted in non-decreasing order.nums
Solution 1 - Move distinct elements to the front of array
In this approach, we are going to apply similar solution as
Implementation steps:
-
We will create three variables
to track the index ofdupIndex array where to move distinct elements to,nums to track current indexed element to check whether next coming elements are duplicates or not, andprevElement to keep track how many times the element is repeated.count -
We will initialize
, since element at 0th index will not have any elements before that,dupIndex =1 , andprevElement = nums[0] , since we have taken the first element.count = 1 -
Then, we will loop through elements of
array from 1st index and check:nums-
If
is not same asnums[i] , that means they are not duplicates, so move element at indexprevElement toi and incrementdupIndex , and update thedupIndex with current value, also resetprevElement .count = 1 -
If
is same asnums[i] , and theprevElement is below 2, then move element at indexcount toi and incrementdupIndex , and incrementdupIndex .count
-
If
-
Finally, return
, it will be at the position where all distinct elements are moved to its left side.dupIndex
import java.util.Arrays;
public class RemoveDuplicatesFromSortedArrayII {
static int removeDuplicates(int[] nums) {
if (nums.length <= 2) {
return nums.length;
}
int dupIndex = 1;
int prevElement = nums[0];
int count = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] == prevElement && count<2) {
nums[dupIndex++] = nums[i];
count++;
} else if (nums[i] != prevElement) {
prevElement = nums[i];
nums[dupIndex++] = nums[i];
count =1;
}
}
return dupIndex;
}
public static void main(String[] args) {
int[] nums = new int[]{1, 1, 2};
System.out.println(removeDuplicates(nums));
System.out.println("Nums after : " + Arrays.toString(nums));
nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(removeDuplicates(nums));
System.out.println("Nums after : " + Arrays.toString(nums));
nums = new int[]{1,1,1,2,2,3};
System.out.println(removeDuplicates(nums));
System.out.println("Nums after : " + Arrays.toString(nums));
}
}
Simplified version of above program:
Here is the simplified version of above program, for illustration purpose we have used prevElementcountdupIndex -2dupIndexdupIndex
import java.util.Arrays;
public class RemoveDuplicatesFromSortedArrayII {
static int removeDuplicates_Simplified(int[] nums) {
if (nums.length <= 2) {
return nums.length;
}
int dupIndex = 2;
for (int i = 2; i < nums.length; i++) {
if (nums[i] != nums[dupIndex -2]) {
nums[dupIndex++] = nums[i];
}
}
return dupIndex;
}
public static void main(String[] args) {
System.out.println("########### Remove duplicates logic simplified ############");
int[] nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(removeDuplicates_Simplified(nums));
System.out.println("Nums after : " + Arrays.toString(nums));
nums = new int[]{1,1,1,2,2,3};
System.out.println(removeDuplicates_Simplified(nums));
System.out.println("Nums after : " + Arrays.toString(nums));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where nnums
Space complexity: O(1).
Above implementations source code can be found at