# Why here Duplicate is coming 14 , because only 7 Duplicate numbers are there

edited Aug 27, 2023
#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]);       //Length is total number of element present in Array

int Duplicate = 0 ;

for(int i = 0 ; i<length-1 ; i++)

{

if(arr[i]==arr[i+1])

{

for(int k = i; k<length-1 ; k++)       //This is loop is for shifting by one

{

arr[k] = arr[k+1];

}

Duplicate++;         //Here if arr[i]==arr[i+1] then value of duplicate integer increase by one

}

}

cout<<Duplicate<<endl;           //Printing number of Duplicate present in Array.

for(int i = 0 ; i<length-Duplicate ; i++)             //It will printing New Array after Removal of Duplicate element from Array

{

cout<<arr[i]<<" ";

}

return 0 ;

}

+1 vote
answered Aug 26, 2023 by (89,510 points)
edited Aug 27, 2023 by Peter Minarik

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.

commented Aug 27, 2023 by (190 points)
In this question I am trying to remove duplicate from sorted array .
Also in this program I am trying to find number of duplicate present in this program.
commented Aug 27, 2023 by (89,510 points)
Gotcha. I've updated my original answer according to this.