# Remove duplicate from sorted array . Also find number of duplicate present.(Why I get Duplicate 14 not 7)

+1 vote
#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);                   //Length of 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 ;

}

answered Aug 29 by (200 points)
Because each time it shifts, it creates new duplicate of last digit which is 13,and as it loops upto the length it checks all those extra seven 13s also and increment Duplicate.

So to avoid this, length should also be decremented.

Now after loop ends, length=length of array after removal of duplicates

last loop condition should be upto length-1;

Correct code is below

#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); //Length of 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];

}length--;

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 ; i++) //It will printing New Array after Removal of Duplicate element from Array

{

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

}

return 0 ;

}
answered Sep 1 by (140 points)

The code you provided is attempting to remove duplicates from an array by shifting elements. However, there's an issue in the code that leads to incorrect results. The problem is with the inner loop where you're shifting the elements of the array. Let's break down what's happening:

1. The initial array is: {1,2,3,4,4,5,6,6,7,7,8,8,9,9,10,11,11,12,12,13}.
2. As you iterate through the array, you encounter the first duplicate at index 3 (value 4) and enter the inner loop to shift elements.
3. After the inner loop completes, the array becomes: {1,2,3,4,5,6,6,7,7,8,8,9,9,10,11,11,12,12,13,13}.
4. The next duplicate is encountered at index 6 (value 6), and you again enter the inner loop to shift elements.
5. After this inner loop completes, the array becomes: {1,2,3,4,5,6,7,7,8,8,9,9,10,11,11,12,12,13,13,13}.
6. The inner loop continues shifting elements for each encountered duplicate, which results in elements being shifted more than once, causing incorrect behavior and leading to an incorrect count of duplicates.

To fix this issue, you should use a different approach to remove duplicates from the array. One common approach is to use a secondary array to store unique elements as you iterate through the original array. Here's a modified version of your code that uses this approach:

#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);

int uniqueArr[length];
int uniqueCount = 0;

for(int i = 0 ; i < length ; i++)
{
bool isDuplicate = false;

for(int j = 0; j < uniqueCount; j++)
{
if(arr[i] == uniqueArr[j])
{
isDuplicate = true;
break;
}
}

if(!isDuplicate)
{
uniqueArr[uniqueCount] = arr[i];
uniqueCount++;
}
}

cout << "Number of duplicates: " << length - uniqueCount << endl;

cout << "Array after removing duplicates: ";
for(int i = 0 ; i < uniqueCount ; i++)
{
cout << uniqueArr[i] << " ";
}

return 0;
}