Challenge - 5 Problems
Remove Duplicates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of removing duplicates from sorted array
What is the output of the following C code that removes duplicates from a sorted array using two pointers?
DSA C
int removeDuplicates(int* nums, int numsSize) { if (numsSize == 0) return 0; int i = 0; for (int j = 1; j < numsSize; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; } int main() { int nums[] = {1,1,2,2,3,4,4,5}; int size = sizeof(nums)/sizeof(nums[0]); int newSize = removeDuplicates(nums, size); for (int i = 0; i < newSize; i++) { printf("%d ", nums[i]); } return 0; }
Attempts:
2 left
💡 Hint
Focus on how the two pointers i and j move and how duplicates are skipped.
✗ Incorrect
The code uses two pointers: i tracks the position of the last unique element, and j scans through the array. When nums[j] is different from nums[i], i is incremented and nums[i] is updated to nums[j]. This way, duplicates are overwritten and the array's first part contains unique elements.
❓ Predict Output
intermediate2:00remaining
Value of newSize after removing duplicates
What is the value of newSize after running the removeDuplicates function on the array {0,0,1,1,1,2,2,3,3,4}?
DSA C
int removeDuplicates(int* nums, int numsSize) { if (numsSize == 0) return 0; int i = 0; for (int j = 1; j < numsSize; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; } int main() { int nums[] = {0,0,1,1,1,2,2,3,3,4}; int size = sizeof(nums)/sizeof(nums[0]); int newSize = removeDuplicates(nums, size); printf("%d", newSize); return 0; }
Attempts:
2 left
💡 Hint
Count how many unique numbers are in the array.
✗ Incorrect
The unique numbers are 0,1,2,3,4 which are 5 elements, but since indexing starts at 0 and the function returns i+1, the result is 5.
🔧 Debug
advanced2:00remaining
Identify the bug causing incorrect output
The following code is intended to remove duplicates from a sorted array but produces incorrect output. What is the bug?
DSA C
int removeDuplicates(int* nums, int numsSize) { int i = 0; for (int j = 1; j < numsSize; j++) { if (nums[j] != nums[i]) { nums[i] = nums[j]; i++; } } return i; }
Attempts:
2 left
💡 Hint
Check the order of assignment and increment of i.
✗ Incorrect
The code overwrites nums[i] before incrementing i, causing duplicates to be overwritten incorrectly. Incrementing i first then assigning nums[i] = nums[j] fixes the logic.
🧠 Conceptual
advanced2:00remaining
Why does the two-pointer approach work for removing duplicates in a sorted array?
Why is the two-pointer technique effective for removing duplicates from a sorted array?
Attempts:
2 left
💡 Hint
Think about how sorting affects the position of duplicates.
✗ Incorrect
In a sorted array, duplicates appear next to each other. One pointer keeps track of the last unique element, and the other scans forward to find new unique elements to copy forward.
❓ Predict Output
expert2:00remaining
Output after removing duplicates and printing array
What is the output of the following C code snippet?
DSA C
int removeDuplicates(int* nums, int numsSize) { if (numsSize == 0) return 0; int i = 0; for (int j = 1; j < numsSize; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; } int main() { int nums[] = {2,2,2,3,3,4,5,5,5,6}; int size = sizeof(nums)/sizeof(nums[0]); int newSize = removeDuplicates(nums, size); for (int i = 0; i < newSize; i++) { printf("%d ", nums[i]); } return 0; }
Attempts:
2 left
💡 Hint
Trace how the two pointers move and update the array.
✗ Incorrect
The function removes duplicates by overwriting repeated elements. The final unique elements are 2,3,4,5,6 printed in order.
