Remove Duplicates from Sorted Array | Leetcode #26 | Easy

Rishikesh Dhokare
3 min readJan 9, 2021

--

In this post I will discuss the solution to the leetcode problem — Remove Duplicates from Sorted Array

Problem:

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

Example 1:

Input: nums = [1,1,2]
Output: 2, nums = [1,2]
Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length.

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 length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.

Constraints:

  • 0 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • nums is sorted in ascending order.

Solution:

There are 2 key pieces of information in the problem statement.

  1. The input array is sorted
  2. We can not allocate extra space.

If the problem allowed to have another array for storing output, then it would have been a piece of cake and you could solve it with just one iteration. But doing it in space is a really nice catch. Let’s jump to the solution then.

When we say “doing it in space”, we need to modify existing input array such that it does not have duplicates anymore. That means we need to iterate over the array at least once for sure. In worst case scenario, the array does not have any duplicates so we will have to return the length of the input array as output.

What we can do when the array has duplicates is — keep moving forward in the array as long as the current element and the previous elements are same otherwise store the next element at the current index. Sounds confusing? Let’s look at the steps.

  1. As a base condition, if the nums array is empty, we can straightaway return 0 as output.
  2. We can iterate the array with 2 pointers. Let’s call them slow and fast and initialize both of them to 1 as we will start the array iteration from the second element of the array.
  3. While iterating the input array, we compare current element with the previous element. Since we are starting with index 1, there is no problem of running out of bound.
  4. If they are not equal, we store the element at index fast to the index of slow and increment slow by one. Otherwise slow remains the same.
  5. Return slow as the output as that is the count of distinct elements in the array.

Here is how the code looks like —

class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int slow = 1;
for (int fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}

Hope this helps! Happy coding! 🙂

If you think the solution can be improved or misses something, feel free to comment. There is always some room for improvement.

Find the solutions to the leetcode problems here — https://github.com/rishikeshdhokare/leetcode-problems

--

--

Rishikesh Dhokare
Rishikesh Dhokare

Written by Rishikesh Dhokare

I am a Software Engineer from India and working in Berlin, Germany. I write about technology, my experiences in Germany, travel in Europe.

No responses yet