80. Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II (LeetCode 80) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the In-place Array Modification subpattern.
How to solve Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II is a In-Place Array Modification problem, part of Two Pointers. Learn in-place array modification using two pointers. Remove duplicates, move zeroes, and sort colors efficiently.
Work towards the solution in these steps:
- Since the array is sorted and you need to modify it in-place, consider how you can use pointers or indices to track positions while iterating through the array.
- Think about maintaining two pointers: one for reading the array and one for writing the allowed elements to preserve the order and overwrite duplicates.
- Because each element can appear at most twice, consider how you can determine if the current element has appeared fewer than two times before placing it in the write position.
The reference solution runs in O(n) time and O(1) space.
The Remove Duplicates from Sorted Array II solution and editorial walks through the full approach with reference code.
Problem statement
Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
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 nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
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:
Input: nums = [1,1,1,2,2,3] 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:
Input: nums = [0,0,1,1,1,1,2,3,3] 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 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order.
Topics and companies
Topics: Array, Two Pointers.
Reported in interviews at Microsoft.
How to practise Remove Duplicates from Sorted Array II on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Remove Duplicates from Sorted Array II in the code editor, or read the Remove Duplicates from Sorted Array II editorial for a worked solution with its approach and complexity analysis.
Where Remove Duplicates from Sorted Array II sits in the DSA pattern sheet
Problems related to Remove Duplicates from Sorted Array II
Other problems that use the same In-place Array Modification and Two Pointers techniques:
- Intersection of Two Arrays — Easy, same subpattern
- Merge Strings Alternately — Easy, same subpattern
- Move Pieces to Obtain a String — Medium, same subpattern
- Move Zeroes — Easy, same subpattern
- Remove Duplicates from Sorted Array — Easy, same subpattern
- Remove Element — Easy, same subpattern
Remove Duplicates from Sorted Array II FAQ
What is the LeetCode number of Remove Duplicates from Sorted Array II?
Remove Duplicates from Sorted Array II is LeetCode problem 80, rated Medium.
What pattern is used to solve Remove Duplicates from Sorted Array II?
Remove Duplicates from Sorted Array II is a In-Place Array Modification problem. It belongs to the Two Pointers pattern, in the In-place Array Modification subpattern.
What is the time complexity of Remove Duplicates from Sorted Array II?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Remove Duplicates from Sorted Array II?
Remove Duplicates from Sorted Array II has been reported in interviews at Microsoft.