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.

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

+2 votes
asked Aug 26, 2023 by Prince (190 points)
edited Aug 27, 2023 by Prince
#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 Answer

+1 vote
answered Aug 26, 2023 by Peter Minarik (86,580 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 Prince (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 Peter Minarik (86,580 points)
Gotcha. I've updated my original answer according to this.

Please, have a look.
commented Aug 27, 2023 by Peter Minarik (86,580 points)
edited Aug 28, 2023 by Peter Minarik
Why do you need the duplicate topic https://question.onlinegdb.com/14914/remove-duplicate-sorted-number-duplicate-present-duplicate?

Doesn't my updated answer explain how to fix things?

It's generally a good idea to keep your questions around the same topic in the same thread.
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.
...