Difficulty: Easy Asked in: Google, Facebook, Microsoft, Amazon, Morgan Stanley
Key takeaway: This problem is an excellent way to learn optimisation using fast and slow pointers. We have discussed two in-place solutions that take O(n) time.
We are given a sorted array and need to write a program to remove duplicate elements so that there is a single occurrence of each element in the array. Note: After removing duplicates, we need to return the length of the array containing unique elements. It doesn't matter what we leave beyond the new length.
Input: X[] = [2, 2, 2, 2, 2, 3, 3, 3], Output: 2, X[] = [2, 3]
Explanation: Our function should return length = 2, with the first two elements of X[] being 2 and 3, respectively. It doesn't matter what we leave beyond the returned length.
Input: X[] = [1, 2, 2, 3, 4, 4, 4, 5, 5], Output: 5, X[] = [1, 2, 3, 4, 5]
Explanation: Our function should return length = 5, with the first five elements of X[] being 1, 2, 3, 4, and 5, respectively.
One idea would be to store unique elements in extra memory and copy them back to the original array. By the end of this process, we will return the new length of the input array.
removeDuplicatesArray(X, n)
if n == 0 or n == 1
return n
unique = array of size n
count = 0
// Keep each element that differs from the next one
for i from 0 to n - 2
if X[i] != X[i + 1]
unique[count] = X[i]
count = count + 1
// Add the final element
unique[count] = X[n - 1]
count = count + 1
// Copy the unique elements back into X
for i from 0 to count - 1
X[i] = unique[i]
return countWe are linearly traversing the array twice, so the time complexity = Traversing the array to store unique elements + Traversing the array to copy unique elements = O(n) + O(count) = O(n). The value of count is equal to n in the worst case. Space complexity = O(n) for the extra array unique[n].
Can we use the sorted array property to solve this problem in place? Let's think.
In a sorted array, all duplicate elements will be placed adjacent to each other. So, we can easily track unique elements and place them into the front positions of the array. For this purpose, we use one fast pointer to scan the array and a slow pointer to track the position of unique elements.
removeDuplicatesArray(X, n)
if n == 0
return 0
slow = 0
fast = 1
// Use fast to scan the array and slow to mark the end
// of the unique elements
while fast < n
if X[fast] != X[slow]
slow = slow + 1
X[slow] = X[fast]
fast = fast + 1
// Return the number of unique elements
return slow + 1We are doing a single scan of the array, so the time complexity is O(n). We are just using two extra variables, so the space complexity is O(1).
Here is another idea to solve the problem by counting duplicates in the array.
removeDuplicatesArray(X, n)
duplicateCount = 0
// Scan the sorted array and count repeated elements
for i from 1 to n - 1
if X[i] == X[i - 1]
duplicateCount = duplicateCount + 1
else
// Move each unique element to its new position
X[i - duplicateCount] = X[i]
// Return the number of unique elements
return n - duplicateCountWe are doing a single scan of the array, so the time complexity is O(n). The space complexity is O(1), and we are just using one extra variable.
If you have any queries/doubts/feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy algorithms!