My simple C++ solution. O(n) time and O(1) space


  • 2
    M

    class Solution {
    public:
    int removeElement(vector<int>& nums, int val) {

        int count=0;
        
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]!=val)
            {
                nums[count] = nums[i];
                count++;
            }
        }
        
        return (count);
    }
    

    };


Log in to reply
 

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