Can anyone tell me why i am getting runtime error?

my code:-class Solution {
public:
int removeDuplicates(vector& nums) {
int cur=nums[0];
if(nums.size()==1)
return nums.size();
if(nums.size()==0)
return 0;
for(auto it=nums.begin()+1;it!=nums.end();++it)
{

        if(*(it)==cur){
            nums.erase(it);
            it--;
            }
        else
            cur=*it;
            
    }
    return nums.size();
    
}

};

format your code by ``` before and after code.

1 Like
int cur=nums[0];

If nums is empty, this will throw an error.
Add this before initializing cur

if(nums.size()==0)
return 0;