LeetCode | Remove Duplicates from Sorted Array | Facebook Interview Questions | Geek Hacker

Reem Alattas
Geek Hacker
Published in
3 min readAug 21, 2021

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.

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

Custom Judge

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementationassert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

Constraints

  • 0 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

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 (hence they are underscores).

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 (hence they are underscores).

Analysis

We need to remove duplicates by updating the array in-place, which means we have to do all the manipulations in the same array without using any other data structure. Then, we have to return the length of the updated array.

Since the array is sorted, all duplicate elements will be adjacent to each other. E.g., this array [1,2,2,3,4,4,4,5,5,6,7].

We can solve this problem by traversing the input array to check if the current and the next element are equal, while keeping track of the unique elements count.

Algorithm

  1. If the array has 0 or only 1 element, then no further modification is needed. Return the array length.
  2. Else, declare and initialize j to keep track of unique elements count.
  3. Traverse the array.
  4. Compare the current element with the next element.
  5. If they are equal, then skip.
  6. Else, place the next (different) element next to the current element.
  7. Return j that indicates the modified array length and the number of unique elements.

Python 3 Code

def removeDuplicates(self, nums):
n = len(nums)
# Return, if array is empty or contains a single element
if n == 0 or n == 1:
return n
# To store index of the next unique element
j = 0
for i in range(0, n-1):
if nums[i] != nums[i+1]:
nums[j] = nums[i]
j += 1
nums[j] = nums[n-1]
j += 1
return j

Complexity

Time Complexity

O(n) because we are traversing an array of n elements once.

Space Complexity

O(1) because we cannot use external data structure due to the problem requirements.

For more LeetCode problems’ solutions, visit my GitHub repo.

If you enjoyed reading this article, please recommend and share it to help others find it!

--

--