Remove Duplicates from Sorted Array


26. Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return 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.


int removeDuplicates(int* nums, int numsSize) {
    if (numsSize == 0) return 0;
    int index = 0;
    for (int i = 1; i < numsSize; i++) {
        if (nums[index] != nums[i]) {
            nums[index] = nums[i];
    return index + 1;

Leave a Reply

Your email address will not be published. Required fields are marked *