LOGIC : First we find out the numbers which is occuring in the array for more than twice and then we are replacing it with INT_MAX simultaneously and also counting how many INT_MAX we are doing, let's call it maxcount . At last, after traversing the array, we sort the array so, all the INT_MAX replaced at the end. We got our modified nums array, and the number of elements in the modified array is nums.size()-maxcount. That is it!!
NOTE -> We are converting more than twice numbers into INT_MAX because the constraint of nums[i] << INT_MAX.
If you like this solution please upvote it.....thanks :)
C++ Code:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int x=0, count=0, maxcount=0;
for(int i=0; i<nums.size(); i++)
{
if(x==nums[i])
count++;
else
{
x = nums[i];
count=1;
}
if(count >= 3)
{
nums[i]=INT_MAX;
maxcount++;
}
}
sort(nums.begin(), nums.end());
return nums.size()-maxcount;
}
};