Putting elements form the end of array instead of element that should be removed


  • 0
    B

    int removeElement(int* nums, int numsSize, int val) {
    int i;

    for (i=0; i<numsSize; i++) {
        if (nums[i] == val) {
            while (numsSize-1 > i && nums[numsSize-1] == val) {
                numsSize--;
            }
            nums[i] = nums[numsSize-1];
            numsSize--;
        }
    }
    
    return numsSize;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.