vector<int>delete_nth(vector<int>arr,intocc){// stores the numbers and its corresponding count in answer vectorunordered_map<int,int>numCount;// the answer vectorvector<int>ans;// if count of any number is less than occ,// only then add it in ansfor(intnum:arr){if(numCount[num]<occ){ans.push_back(num);numCount[num]++;}}returnans;}
ans vector contains each number only up to occ times. So, occ can never be negative. If user passed a negative number as occ, then ans vector will be empty.
If given numbers are negative, still there should not be any problem. Example,
Here is C++ solution,
What if given numbers are not positive ?
ans
vector contains each number only up toocc
times. So,occ
can never be negative. If user passed a negative number asocc
, thenans
vector will be empty.If given numbers are negative, still there should not be any problem. Example,