0

I have an array a={1,2,3,3,2,2,3,3} and I need to remove the duplicates like this:

1: a={1,2,2,2,3,3}
2: a={1,2,3,3}
3: a={1,2}

I need to remove 2 consecutive duplicates: (1,2,3,3 will be 1,2), (1,2,2,2 will be 1,2).

Here is my try, but as you can see, I need some help.

#include <iostream>

int main()
{
    int n;
    std::cin >> n;

    int a[n];
    for (int i = 0; i < n; i++)
        std::cin >> a[i];

    int i, j;
    for (i = 0; i < n; i++)
        if (a[i] == a[i + 1]) {
            for (j = i + 1; j < n; j++)
                a[j - 1] = a[j];
            n--;
            i--;
        }

    if (n != 0)
        for (int i = 0; i < n; i++)
            std::cout << a[i] << " ";

    return 0;
}

My problem is that I don't know how to remove 2 consecutive values. After multiple tries, I can't resolve this. Thank you in advance!

8
  • Why do you get rid of 3 in the last step? Commented Jun 12, 2018 at 20:11
  • 2
    std::unique combined with std::remove/std::erase (and maybe std::sort) is what springs to mind.. Definitely no need for all those open-coded loops - std algorithms will do what you need. Commented Jun 12, 2018 at 20:11
  • 1
    @NathanOliver The objective is to eliminate duplicates in pairs. So if you had an even number of consecutive duplicates, you remove them all. If you had an odd number, one remains. At least that's how I understand it. Commented Jun 12, 2018 at 20:11
  • 1
    int a[n] is a non-standard variable length array. It's not supported by c++, though some compilers support it as an extension. Using int a[n] is not portable. Commented Jun 12, 2018 at 20:14
  • 1
    To everyone suggesting std::unique and std::set, the problem is not to remove all but one duplicates. It's to remove consecutive duplicates pair-wise. Commented Jun 12, 2018 at 20:26

2 Answers 2

1

I'm not going to write code for you, but here are my thoughts.

First, write a function to check if there even exists "consecutive duplicates":

//returns true if there are no consecutive duplicates within the array, false otherwise
func noConsecDups(arr a)
for int i = 0, i <= a.length-2, i++
if a[i] = a[i++]
return false
end of if
end of loop
return true
end function

Now, write a function that removes the consecutive duplicates recursively (might not have to do it recursively, that's just my initial thought) while checking to see if you even need to remove any!

//function that takes an array as input and returns the array with all consecutive duplicates removed
func removeConsecDups(arr a)
if a.length is 1, return a
if a.length is 2 and a[0] != a[1], return a
if(noConsecDups(a)) then there are no consecutive duplicates, return a
otherwise look through the array and just remove the first consecutive duplicates
for int j = 0, j <= a.length-2, j++
if a[j] = a[j+1]
remove a[j+1]
remove a[j]
break
end if statement
end loop
recursively call removeConsecDups(a)
end function
Sign up to request clarification or add additional context in comments.

Comments

0

If you just need the final result (an array with no consecutive duplicates left) then your best bet is probably to use a stack and just traverse the whole input array once, comparing the values to the stack top and poping the duplicates off the stack.

If you need to print out the array state after every intermediate step, then @BarronDuBois's suggestion is the way to go.

Either way the code itself should be simple enough, I'd be glad to help with any specific issue.

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.