Introduction
The Remove Duplicates from Sorted Array problem involves removing duplicate elements from a sorted array such that each unique element appears only once.
Given a sorted array arr[] of size n, the task is to remove duplicates in-place and return the number of unique elements.
This is a popular array problem that helps in understanding Two Pointer Technique, in-place array modification, and efficient traversal.
Example
Input: arr[] = [1, 1, 2, 2, 3, 4, 4]Output: [1, 2, 3, 4]
Unique Count = 4
Explanation:
Duplicate elements are removed.
Only unique elements remain in the array.
Input: arr[] = [5, 5, 5, 5]
Output: [5]
Unique Count = 1
Explanation:
All elements are duplicates except the first occurrence.
Constraints
Array is sorted in non-decreasing order1 <= n <= 10^5-10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force (Using Extra Array)
Explanation
The simplest way to solve this problem is:
- Create another array
- Traverse the original array
- Store only unique elements
Since the array is sorted, duplicates appear consecutively, making them easy to detect.
This approach is easy to understand but uses extra space.
Steps
- Create a new array.
- Insert the first element.
- Traverse the remaining array.
- If current element is different from previous element:
- add it to result array
- Return the unique elements.
Dry Run
Input Array: [1, 1, 2, 2, 3, 4, 4]Insert first element:
Result = [1]
Traverse next 1:
Duplicate element
Skip it
Traverse 2:
Unique element
Insert 2
Result = [1, 2]
Traverse next 2:
Duplicate element
Skip it
Traverse 3:
Insert 3
Result = [1, 2, 3]
Traverse 4:
Insert 4
Result = [1, 2, 3, 4]
Final Result:
[1, 2, 3, 4]
Brute Force Code
Complexity Analysis
Time Complexity: O(n)Explanation:
The array is traversed once.
Space Complexity: O(n)
Explanation:
An extra array is used to store unique elements.
Approach 2 : Optimized Solution (Two Pointer Technique)
Explanation
Instead of using another array, we can remove duplicates in-place using the Two Pointer Technique.
The idea is:
- One pointer tracks position for unique elements
- Another pointer traverses the array
- Whenever a new unique element is found:
- place it at the correct position
This avoids extra space usage.
Steps
- Initialize pointer j=1.
- Traverse the array from index 1.
- Compare current element with previous unique element.
- If current element is unique:
- place it at index j.
- increment j
- First j elements become unique elements.
Dry Run
Input Array: [1, 1, 2, 2, 3, 4, 4]Initially:
j = 1 Traverse second 1:
Duplicate element
Skip it
Traverse 2:
Unique element found
Place 2 at index 1
Array becomes:
[1, 2, 2, 2, 3, 4, 4]
Increment j = 2
Traverse next 2:
Duplicate element
Skip it
Traverse 3:
Place 3 at index 2
Array becomes:
[1, 2, 3, 2, 3, 4, 4]
Increment j = 3
Traverse 4:
Place 4 at index 3
Final Unique Elements:
[1, 2, 3, 4]
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: Every element is visited exactly once.
Space Complexity: O(n)
Explanation: Only pointer variables are required.
Edge Cases
- Array contains all duplicates
- Array contains no duplicates
- Array contains only one element
- Array contains negative numbers
Why This Problem is Important
This problem helps in understanding:
- Two Pointer Technique
- In-place array modification
- Efficient traversal
- Duplicate handling
- Space optimization
It is one of the most important beginner-level array manipulation problems.
Real-World Applications
Duplicate removal is used in:
- Database systems
- Search engines
- Data cleaning systems
- Log processing
- Analytics pipelines
Common Mistakes
- Incorrect pointer movement
- Overwriting elements incorrectly
- Forgetting sorted array property
- Returning incorrect unique count
Interview Tips
Interviewers often expect:
- In-place modification
- O(n) traversal
- Proper Two Pointer usage
Always explain why sorted arrays make duplicate detection easier.
Related Questions
- Move Zeroes
- Remove Element
- Merge Sorted Arrays
- Sort Colors
- Rearrange Array
Final Takeaway
The Remove Duplicates from Sorted Array problem is a fundamental Two Pointer problem that teaches efficient in-place array modification and duplicate handling techniques. Understanding this problem builds a strong foundation for advanced array manipulation and optimization problems.