Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

+1 vote
asked Aug 27, 2023 by Prince (190 points)
#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 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 ;

}

2 Answers

0 votes
answered Aug 29, 2023 by AmritaB (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[0]); //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 ;

}
0 votes
answered Sep 1, 2023 by 502_D2d_Sagar Koradiya (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[0]);

    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;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...