Remove Duplicates From Sorted Array Solution for Accenture

Company: Accenture · 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.

Key Understanding:

Examples

Example 1:

Input: nums = [1,1,2]

Output: 2, nums = [1,2,_]

Explanation:

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]

Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]

Explanation:

Example 3:

Input: nums = [1,2,3,4,5]

Output: 5, nums = [1,2,3,4,5]

Explanation:

Example 4:

Input: nums = [1,1,1,1,1]

Output: 1, nums = [1,_,_,_,_]

Explanation:

Intuition

The key insight is that since the array is sorted, all duplicates will be adjacent. We can use two pointers to keep track of where to write the next unique element.

Core Concept:

Why Two Pointers Work:

Approach

Approach 1: Two Pointers (Optimal)

Algorithm:

1. Handle edge case: if array is empty, return 0

2. Initialize write pointer at index 1 (first element is always unique)

3. Iterate through array starting from index 1 (read pointer)

4. For each element:

5. Return write pointer (length of unique elements)

Why This Works:

Complexity Analysis:

Approach 2: Using Extra Array

Algorithm:

1. Create a temporary array to store unique elements

2. Copy the first element to temp array

3. Iterate through original array starting from index 1

4. If current element is different from previous element:

5. Copy temp array back to original array

6. Return length of temp array

Why This Works:

Complexity Analysis:

Approach 3: Count and Shift

Algorithm:

1. Initialize unique count to 1 (first element is always unique)

2. Iterate through array starting from index 1

3. If current element is different from previous element:

4. Return unique count

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: nums = [1,1,2]

Two Pointers Approach:

Initial State: write = 1, nums = [1,1,2]

Step 1: read = 1, write = 1

Step 2: read = 2, write = 1

Step 3: read = 3 (end of array)

Final Result: Length = 2, nums = [1,2,2] (first 2 elements are unique)

Extra Array Approach:

Step 1: Create temp array

Step 2: Copy first element

Step 3: Process remaining elements

Step 4: Copy back to original

Final Result: Length = 2, nums = [1,2,2]

Key Insights

1. Sorted Array Property: Since array is sorted, duplicates are adjacent

2. First Element: Always unique and stays in place

3. Two Pointers: Write pointer tracks where to place next unique element

4. In-Place Modification: We can modify the array without extra space

5. Return Length: The function should return the new length, not the modified array

Edge Cases

1. Empty Array: Return 0

2. Single Element: Return 1 (always unique)

3. All Duplicates: Return 1 (only one unique element)

4. No Duplicates: Return original length (no changes needed)

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) - We traverse the array once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Using Extra Array

Time Complexity: O(n) - We traverse the array twice

Space Complexity: O(n) - We need extra array

Advantages:

Disadvantages:

Solution 3: Count and Shift

Time Complexity: O(n²) - We may need to shift elements

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

More Accenture coding questions