Help! Need to delete repeats

I am writing a function to delete repeats in an array. The array elements are entered at the keyboard (array may not be totally filled). I need to compare all elements and if any are repeated delete them. I'm using Visual C++. Here is some of what I've got so far.
int delete_repeats(char letters[], int size)
{
int index, i(0), repeats(0);
for(index = 0; index < size; index++)
{
if (letters[index] == letters[index + 1])
{
//Need help here!
}
repeats++;
}
return repeats;
}

Comments

  • : I am writing a function to delete repeats in an array. The array elements are entered at the keyboard (array may not be totally filled). I need to compare all elements and if any are repeated delete them. I'm using Visual C++. Here is some of what I've got so far.

    : int delete_repeats(char letters[], int size)
    : {
    : int index, i(0), repeats(0);
    : for(index = 0; index < size; index++)
    : {
    : if (letters[index] == letters[index + 1])
    : {
    : //Need help here!
    : }
    : repeats++;
    : }
    : return repeats;
    : }
    :

    Well, the most proper way, generally-coding-wise, would be to create STL vector around the letters and use the unique algorithm routine to do the work for you.

    However, I'll assume that you need to do this the "hard" way or just don't want to use STL and I'll start commenting on your code.

    Firstly, for built-in types you should not be using constructor syntax to initialize them.

    IE int index,i=0,repeats=0;

    Next, your code is fine for "007", but what about poor MI6 agent "010". Both of these contain repeats, but your code only checks for ADJACENT repeats. The algorithm you'll need to use for each value of the array you'll need to check every location in the array for duplicates. So you'll need nested for-loops. As for "deleting" the values from the array, that's kind of tricky to do cleanly. The simplest solution (besides the one above) is to copy the value to an intermediate array if they are unique. Then at the end copy the elements back into the original array.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion