Remove Duplicates From Sorted Array Solution for Capgemini
Company: Capgemini · Difficulty: Easy · Topics: Array, Two Pointers
Problem Statement
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. 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.
Examples
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation:
Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k.
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation:
Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k.
Example 3:
Input: nums = [1,1,1,1,1]
Output: 1, nums = [1,_,_,_,_]
Explanation:
Your function should return k = 1, with the first element of nums being 1. All duplicates are removed.
Example 4:
Input: nums = [1,2,3,4,5]
Output: 5, nums = [1,2,3,4,5]
Explanation:
Your function should return k = 5, with all elements being unique and in the same order.
Intuition
The key insight is to use two pointers - one to track the position where we should place the next unique element, and another to scan through the array.
This works because:
- The array is sorted, so duplicates are adjacent
- We can use a slow pointer to track where to place unique elements
- We use a fast pointer to scan for the next unique element
- When we find a different element, we place it at the slow pointer position
Approach
Approach 1: Two Pointers (Optimal)
1. Initialize two pointers: i = 0 (for placing unique elements) and j = 1 (for scanning)
2. While j < array length:
- If nums[j] != nums[i], increment i and copy nums[j] to nums[i]
- Increment j
3. Return i + 1 (the number of unique elements)
Approach 2: Using Extra Array
1. Create a temporary array to store unique elements
2. Copy unique elements to the temp array
3. Copy back to the original array
4. Return the count of unique elements
Approach 3: Brute Force (Inefficient)
1. For each element, check if it's a duplicate of any previous element
2. If not duplicate, place it at the next position
3. Return the count of unique elements
Dry Run
Let's trace through Example 2: nums = [0,0,1,1,1,2,2,3,3,4]
Two Pointers Approach:
- i = 0, j = 1: nums[1] == nums[0] (0 == 0), j++
- i = 0, j = 2: nums[2] != nums[0] (1 != 0), i++, nums[1] = 1, j++
- i = 1, j = 3: nums[3] == nums[1] (1 == 1), j++
- i = 1, j = 4: nums[4] == nums[1] (1 == 1), j++
- i = 1, j = 5: nums[5] != nums[1] (2 != 1), i++, nums[2] = 2, j++
- i = 2, j = 6: nums[6] == nums[2] (2 == 2), j++
- i = 2, j = 7: nums[7] != nums[2] (3 != 2), i++, nums[3] = 3, j++
- i = 3, j = 8: nums[8] == nums[3] (3 == 3), j++
- i = 3, j = 9: nums[9] != nums[3] (4 != 3), i++, nums[4] = 4, j++
- Return i + 1 = 5
Extra Array Approach:
- Create temp array: []
- Scan array: [0,0,1,1,1,2,2,3,3,4]
- Add unique elements: [0,1,2,3,4]
- Copy back: nums = [0,1,2,3,4,_,_,_,_,_]
- Return 5
Key Insights
1. Sorted Property: Since array is sorted, duplicates are adjacent
2. In-Place Operation: Two pointers allow modification without extra space
3. Slow Pointer: Tracks position for next unique element
4. Fast Pointer: Scans for next different element
5. Efficiency: Single pass through array gives O(n) time complexity
Edge Cases
1. Empty array: Return 0
2. Single element: Return 1
3. All duplicates: Return 1
4. No duplicates: Return array length
5. Large arrays: Handle efficiently with two pointers
Solution
⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.
Solution 1: Two Pointers (Optimal)
Time Complexity: O(n) - Single pass through the array
Space Complexity: O(1) - Constant extra space
Solution 2: Using Extra Array
Time Complexity: O(n) - Two passes through the array
Space Complexity: O(n) - Extra array needed
Solution 3: Brute Force
Time Complexity: O(n²) - For each element, check all previous elements
Space Complexity: O(1) - Constant extra space