Here is a question from codewars.com
Given a list and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
And here is my code:
std::vector<int> deleteNth(std::vector<int> arr, int n)
{
int counting = 0;
int counting2 = 0;
for (int i : arr)
{
for (int j : arr)
{
cout << arr.size() << " " << counting<<" "<<counting2<<endl;
if (i == j)
{
++counting;
if (counting > n) { arr.erase(arr.begin() + counting2); --counting2; }
}
counting2++;
}
counting = 0;
counting2 = 0;
}
return arr;
The basic tests are fine. But when i attemp their random test. It 's a mess.
Expected: equal to [ 8, 32, 32, 8, 8, 26, 26, 8, 19, 26, 26, 19, 26, 26, 19, 8, 8, 19, 26, 8, 8, 19, 32, 32, 26, 50, 19, 32, 32, 32, 19] Actual: [ 8, 32, 32, 8, 8, 26, 26, 8, 19, 26, 26, 19, 26, 26, 19, 8, 8, 19, 26, 8, 8, 19, 32, 26, 50, 19, 32, 32, 19 ]