C++ ||O(n) || easy to understand || two pointer
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
  
        int i=0,cnt=0;
        int j= nums.size()-1;
        if(nums.size()==NULL)
        {
            return NULL;
        }
        jump:
        while(j>i)
        {
            if(nums[i]==val)
            {
            
                if(nums[j]==val)
                {
                    j--; cnt++;
                    goto jump;
                }
                else{
                nums[i]=nums[j];
            
                j--;
                i++;
                cnt++;
                }
            }
            else{
                i++;
            }
        }
        if(nums[i]==val)
        {
          cnt++;  
        }
        cnt=nums.size()-cnt;
        return cnt;
    }
};
Comments (0)