Bird
0
0
DSA Cprogramming~20 mins

Remove Duplicates from Sorted Array Two Pointer in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Remove Duplicates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A1 2 2 3 4 5
B1 1 2 2 3 4 4 5
C1 2 3 4 5
D1 2 3 4 5 5
Attempts:
2 left
💡 Hint
Focus on how the two pointers i and j move and how duplicates are skipped.
Predict Output
intermediate
2: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;
}
A5
B6
C10
D4
Attempts:
2 left
💡 Hint
Count how many unique numbers are in the array.
🔧 Debug
advanced
2: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;
}
AThe assignment nums[i] = nums[j] should happen after incrementing i.
BThe function should return i + 1 instead of i.
CThe loop should start from j = 0 instead of j = 1.
DThe condition nums[j] != nums[i] should be nums[j] == nums[i].
Attempts:
2 left
💡 Hint
Check the order of assignment and increment of i.
🧠 Conceptual
advanced
2: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?
ABecause the two pointers count the total number of duplicates without modifying the array.
BBecause the two pointers swap elements to sort the array first.
CBecause the two pointers create a new array with unique elements.
DBecause the array is sorted, duplicates are adjacent, so one pointer can track unique elements while the other scans ahead.
Attempts:
2 left
💡 Hint
Think about how sorting affects the position of duplicates.
Predict Output
expert
2: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;
}
A2 2 3 3 4 5 5 6
B2 3 4 5 6
C2 3 3 4 5 6
D2 3 4 5 5 6
Attempts:
2 left
💡 Hint
Trace how the two pointers move and update the array.