This is because the code
for (int k = i; k < length - 1; k++)
{
arr[k] = arr[k + 1];
}
If you comment out the above code, then your program prints out 7 not 14.
What is your program supposed to do?
Is it supposed to calculate how many duplicates are after one another (i.e. 123345 has a duplicate -- "33"; but 123435 is not a duplicate -- the two 3s --, because we are only interested in the same numbers next to each other)? If this is the case, why do you need the above loop (that changes the input, so affects how many duplicates are found)?
Could you please specify what your program is supposed to do? Then we can find out how it should be implemented.
Update
Thank you for providing more details. To remove all the duplicates and check how many duplicates were, your code could be modified like this:
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12, 13 };
int length = sizeof(arr) / sizeof(arr[0]);
int stopIndex = length - 1;
int Duplicate = 0;
for (int i = 0; i < stopIndex; i++)
{
if (arr[i] == arr[i + 1])
{
for (int k = i + 1; k < stopIndex; k++)
{
arr[k] = arr[k + 1];
}
stopIndex--;
Duplicate++;
}
}
cout << "Number of duplicates: " << Duplicate << endl;
cout << "The array after the removal of duplicates: " << arr[0];
for (int i = 1; i <= stopIndex; i++)
cout << ", " << arr[i];
cout << endl;
return 0;
}
The idea is that your original loop went to the total length of the array, even though you were removing elements from it. You should have reduced the length as well (that's what I solved via the stopIndex variable).
Keep in mind that this code only removes duplicates (i.e. two of the same numbers). It will not work correctly if there are 3-of-a-kind numbers in the array.