In this tutorial, we will teach you How you can remove duplicates from sorted array. It’s very commonly asked question in DSA Interview. Also, we will show you Animation to understand the algorithm better way. his problem helps interviewers evaluate your understanding of arrays, in-place modification, and optimized logic using two pointers.
We’ll use C++, Java, Puthon , and JavaScript programming language , you can choose your favorite programming language. We’ll also analyze the time and space complexity, making it easier for you to remember and apply this technique in real interviews. you can solve it on Leetcode platform.
This problem helps to understand about you knowledge of :
- the array traversal skill.
- your logical thinking.
- your knowledge of time & space complexity.
- Optimized logic using two pointer.
Problem Statement
You can read the complete problem statement on the LeetCode platform by clicking here.

Optimized Algorithm – Two Pointer Technique – Explained using Animation
Let an Array nums is given.
Step-by-step Explanation:
- Edge case
- If the array is empty (
nums.length === 0), return0
- If the array is empty (
- Initialize a pointer
- Let
k = 1 - This pointer represents the position of the next unique element
- Let
- Traverse the array
- Start from index
1ton - 1 - Compare current element
nums[i]with previous elementnums[i - 1]
- Start from index
- If elements are different
- It means we found a new unique element
- Place it at index
k - Increment
k
- Return
kkis the count of unique elements- First
kelements of the array are unique and sorted
Dry run example
nums = [1, 1, 2]| i | nums[i] | action |
| 1 | 1 | same as previous -> skip |
| 2 | 2 | Diffrent than previous -> nums[k] = nums[i] i.e 3 and k++ (increament) |
Final array: [1, 2, _]
Output: k = 2
Remove Duplicates From Sorted Array – Using Animation

Solution Using C++, Java, Python, JavaScript
To solve this problem , we have used below programming language
- C++
- Java
- Python
- JavaScript
Each implementation follows the same algorithm, helping you understand how logic remains constant while syntax changes.
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0)
return 0;
int k = 1; // pointer for unique elements
for (int i = 1; i < nums.size(); i++) {
if (nums[i] != nums[i - 1]) {
nums[k] = nums[i];
k++;
}
}
return k;
}public int removeDuplicates(int[] nums) {
if (nums.length == 0)
return 0;
int k = 1; // pointer for unique elements
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[k] = nums[i];
k++;
}
}
return k;
}def removeDuplicates(self, nums):
if len(nums) == 0:
return 0
k = 1 # pointer for unique elements
for i in range(1, len(nums)):
if nums[i] != nums[i - 1]:
nums[k] = nums[i]
k += 1
return kvar removeDuplicates = function(nums) {
if ( nums.length === 0 )
return 0;
let k = 1; //pointer for unique elements
for ( let i = 1; i < nums.length; i++ ) {
if ( nums[i] !== nums[i-1] ) {
nums[k] = nums[i];
k++;
}
}
return k;
};Output:-

Conclusion:-
Thank you for reading this tutorial. Our motivation is to share DSA concepts and problem-solving techniques in a simple and easy-to-understand way. We hope this explanation helped you clearly understand the Remove Duplicates From Sorted Array and its optimized solution.
We encourage you to explore more DSA problems and interview questions on our platform to build strong fundamentals and gain confidence for technical interviews.


