Erasing array element

I need help. Could yall explain what's happening in the code where I left question marks in the comment place ? This is my first time erasing elements. Done this with internets help but couldn't understand what's happening in certain parts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
      for(int i = 0; i != n; i++)
    {
        for(int j = i; j != n; j++) // Made 2 loops to find duplicates
        {
            if(i != j) // Making sure that it doesn't check itself
            {
                if(S[i] == S[j]) // If I find duplicate 
                {
                    n--; /* ?? (I reduce the length of the array? Does it 
                         delete last index or what ? Because I believe I 
                         haven't removed the duplicate yet */
                    for(int k = i; k != n; k++) // ??
                    {
                        S[k] = S[k + 1]; // Shifting numbers to the right side
                    }
                i--; // ????
                }
            }
        }
    }
Last edited on
for(int j = i; j != n; j++) // Made 2 loops to find duplicates
{
if(i != j)
this is kinda derpy. Why not set j = i+1 and skip this check?

n-- the next loop removes the duplicate by shifting over the top of it. the last element in the array will be junk left over, but because n is reduced, you must know to ignore it later on. All n-- really does is eliminate excessive looping off the n variable, you do less work. I don't think it would change the result if you took this line out, but it will take longer.

because of the way he shifted, which over-writes the leading duplicate instead of the trailing one (that is, if yuo had 12233 and and it removed 2, you get 12233 where the bold is shifted and the strikeout is overwritten) you have to back i up. If he had just looped i+1 to remove the second duplicate instead, it would not need this.

conclusion: code is a little clunky and can be improved.

consider that c++ has a built in way to do this as well, look at remove or if on a vector, remove & erase paired.
Last edited on
How come I can't do the same with removing elements that have an odd index ? If it's odd index I shift it to the right side. just like I did with duplicates. But this doesn't seem to work like that. Plus I believe that if(i % 2 == 0) continue; is useless because I could've just done if(i % 2 != 0)

1
2
3
4
5
6
7
8
9
10
11
12
13
    for(int i = 0; i != n; i++)
    {
        if(i % 2 == 0) continue;
        else
        {
            n--;
            for(int k = i; k != n; k++)
            {
                P[k] = P[k + 1];
            }
            i--;
        }
    }
Last edited on
you can do it, but you probably have a bug. I don't see it. print the entire array at the end of every loop, see what it did, look in the code to see what caused the problem, ... repeat until working.

a really simple way to eliminate every other value in a sorted array:
for(i = 0; i < size; i+=2) //or i =1 for the other half
array[i] = maxpossiblevalue; //depends on type, get from <limits>
then re-sort the array.

this data shuffle every loop iteration is extremely inefficient and intractable for large arrays.
Last edited on
Topic archived. No new replies allowed.