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:

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:

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:

Extra Array Approach:

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

More Capgemini coding questions