Deleting repeated characters in an array

I am having a hard time understanding the logic behind comparing variables in an array. I know to create my array but I just cant put my finger on the logic behind how to create a function to remove repeated characters from it. I know I should start the function like this:

void deleteRepeats(array[], int& size)
{
int counter = 0;
for (int i = 0; i<size; i++)
{

}

}

this is where I draw a blank. I know I need to start at the beginning of the array and take the first value and compare it to every other value and then repeat this process with the next value. I think I need a second for-loop, the first to compare the array value against the other values and then the second to progress the array value forward to the next value to start the process again?

Any guidance would be appreciated.
If you can use the algorithm library, it has a function that does that0.

http://en.cppreference.com/w/cpp/algorithm/unique

Otherwise, to do it yourself you probably will need more than 1 loop. You didn't specify a data type for your array. Is it a string (C++ string)? A character array (C string)? An integer array?
firstly, arrays have fixed sizes. Given this, you would want to do 2 things;
- replace the repeated character with some other
- shift all characters after the repeatition one place to the left. append '\0' to the array.

In either case you would need 2 loops as you noticed.
1
2
3
4
5
6
7
for(int i = 0; i < n-1; i++) //select a character
{
    for(int j = i +1; j < n; j++)  //compare
    {
         //do stuff
    }
}


Case 1 is a lot simpler.
Choose a character to replace with: e.g sub = '#'
then in nested loop, do :
1
2
if(A[i] == A[j])
    A[j] = sub;


Case 2 is a good one to try yourself.

Using std::vector is a lot easier in either case.
closed account (SECMoG1T)
We can use nested loops , the first loop would track the current position while the second loop would scan the rest of the array, if a character is repeated we could simply replace it with a space ' ' or erase it in dynamic containers. . Like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  void deleteRepeats (char arr []; int size)
    {
         for (int i=0; i <size; i++)
           {
                 char c=arr [i];
               for(int j=i+1; j <size; j++)
                  {
                     if(c==arr [j])
                       arr [j]=' ';
                   }
             }
      }///complete

  You might also want to format your array so that all spaces are moved to the back

  void moveSpaces (char arr [], int size)
    {
       for (int a=0; a <size; a++)
          {
             if (arr [a]==' ')
                int sp_ind=a;
               for (int b=a+1; b <size; b++)
                 {
                     if (arr [b]!=' ')
                       { std::swap (arr [b], arr [sp_ind]); break;}
                 }
            }
   }


Really sorry for giving you all that.
And FYI +5 for ShadowCode case 2
Last edited on
Thanks for all the help. It is much appreciated. I need to read that link a few times that FG109 provided. I understand the nested loops that Andy1992 provided but I have trouble converting that code to words. Are my comments correct?


void deleteRepeats (char arr []; int size)
{
for (int i=0; i <size; i++)
{
char c=arr [i]; //c represents the value in the array
for(int j=i+1; j <size; j++) //new loop
{
if(c==arr [j]) //if c equals j
arr [j]=' '; //replace j with a space
}
}
}
Topic archived. No new replies allowed.