0

I am testing out this piece of code in c++

#include <iostream>

using namespace std;

int removeDuplicates(int A[], int n) {
    if(n == 0) return 0;

    int index = 0;

    for(int i=0; i<n; i++)
    {
        if(A[index] == A[i])
        {

            continue;
        }
        index++;
        A[index] = A[i];
    }
    cout<<"New array is "<<A[index]<<endl;
    return index+1;
}


int main(){

    int x[10] = {1,1,6};
    int n = 3;
    int z = removeDuplicates(x, n);
    cout<<"Length is "<<z;
}

The result of length is right. However, when cout the new array. The result is only 6. 1 is missing.

Where should I put cout<<"New array is "<<A[index]<<endl;

To give me the result as New array is 16

More code I need to add? Please help, anyone?

2
  • cout<<"New array is "<<A[index]<<endl; outputs the element at position index of A. Commented Feb 27, 2015 at 22:16
  • 2
    Use a for loop to print contents of array. You can't directly print a whole array. Commented Feb 27, 2015 at 22:21

3 Answers 3

4

std::unique and std::erase are your friends.

std::vector is also a good friend.

By the way, your program does remove duplicates correctly; it's just the output statement that doesn't show more of the array than the single value 6.

Sign up to request clarification or add additional context in comments.

Comments

0
for(int i=0; i<n; i++)
{
    if(A[index] == A[i])
    {

        continue;
    }
    index++;
    A[index] = A[i];
}

Let's go trace: A = {1,1,6}; n = 3;

1º i = 0; index = 0 -> a[0] == a[0]? -> Continue

2º i = 1; index = 0 -> a[0] == a[1]? -> Continue

3º i = 2; index = 0 -> a[0] == a[2]? -> Nope, so index++ -> a[1] = a[2];

The new array is: {1,6,6}. index has the value of = 1, so you print:

cout<<"New array is "< 6

You return index = 2 and A = {1,6,6}. This is a correct result, your algorithm works, but the print doesn't have sense. You should print with for in the return:

#include <iostream>

using namespace std;

int removeDuplicates(int A[], int n)
{
    if(n == 0) return 0;

    int index = 0;

    for(int i=0; i<n; i++)
    {
        if(A[index] == A[i])
        {

            continue;
        }
        index++;
        A[index] = A[i];
    }
    return index+1;
}


int main()
{

    int x[10] = {1,1,6};
    int n = 3;
    int size = removeDuplicates(x, n);
    cout<<"Length is "<<size<<endl;

    for (int i = 0; i<size; i++)
    {
        cout<<x[i]<<' ';
    }
}

This works great :). But, you have one function of standar library for this:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{

    int x[10] = {1,1,6};
    int n = 3;
    int* end = unique(x,x+n); // returns reference to the end

    for (int i = 0;(x+i) != end; i++)
    {
        cout<<x[i]<<' ';
    }
}

You can check reference of unique here: http://www.cplusplus.com/reference/algorithm/unique/

1 Comment

Now I understand it. Thank you for your help, amchacon!
0

Instead of using std::unique and std::erase, do it in one line using std::unique_copy and std::ostream_iterator<>. Let me do that:

unique_copy(begin(ar),end(ar),ostream_iterator<T>(cout,"\n"));

where ar is any STL sequence container, and T is its value_type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.