Deleting duplicates

I am having troubles with my deleting_duplicates function at the end and cannot return the correct values/letters. The project is to delete any repeating letters from the initial phrase we are given and then display the new phrase without duplicate letters. Any help would be much appreciated!
P.S. I am a beginner so their will probably be obvious mistakes..

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
using namespace std;

int length(char array[]);
int delete_duplicates(char array[], int size);

int main()
{

	char array[100]
		= "Marry had a little lamb whos fleece was white as snow";
	
	cout << array <<endl;

	int size, short_size;
	size = length(array);
	cout << "size = " << size <<endl;

	short_size = delete_duplicates(array, size);
	cout << "reduced array = " << array <<endl;
	cout << "reduced array size = " << short_size <<endl;

}

int length(char array[])
{
	int i;
	for (i=0; '\0' !=array[i]; i++)
	{
	
	}
	return i;
}

int delete_duplicates( char array[], int size)
{
int i, j, k;
	i= 0;
	j= i+1;

	for (i=0; i<size; i++)
	{
	  for (j=i+1; j<size; j++)
	  {
	    if (array[j] == array[i])
	    {
	      for (k=j+1; k<size; k++)
	      {
	        array[k] == array[j];
		size--;
		j--;
	      }
	    }
	  }
	}
	return i;
}
Last edited on
Eeeeeh, no, not quite. The loops all seem okay, but I believe you were supposed to go down the loop copying values one over to the left, which means that line 49 isn't right. What it's doing is copying whatever is at index j to the rest of the array, which is making your duplication issue worse.

Line 50 should not be in the innermost while loop.

I'm not sure what purpose you're intending line 51 to serve. O.o

-Albatross
Last edited on
I completely got rid of line 51 and changed array[k] to array [k-1]? You are correct when saying the loop is suppose to copy values over to the left, but I am not sure what exactly to include in the body of the delete function. Any pointers/tips/examples will help out tremendously.
help* [/bad-pun]

Changed line 49: Er. Okay, why did you change array[k]? Again, why do you have array[j] in that line?

What did ya do with line 50?

-Albatross
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
29
30
31
32
#include <iostream>

void delete_duplicates( char array[] , int &size )
{
	int duplicates = 0;
	int i = 0;
	char temp;
	while( i < size - duplicates )
	{
		if( array[i] == array[i+1] )
		{
			for( int j = i + 1; j < size - duplicates; ++j )
			{
				temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
			++duplicates;
		}
		++i;
	}
	size -= duplicates;
}

int main() {
	const int SIZE = 54;
	int current_size = SIZE;
	char ch[SIZE] = "Marry had a little lamb whos fleece was white as snow";
	std::cout << ch << " size = " << current_size << std::endl;
	delete_duplicates( ch , current_size );
	std::cout << ch << " size = " << current_size << std::endl;
}
Marry had a little lamb whos fleece was white as snow size = 54
Mary had a litle lamb whos flece was white as snow size = 51
Here is the algorithm I'm working with. I switch line 50 to inside the last for loop

For each character in the array
For each character beyond the one the outer loop is on
if the character ffrom the inner loop matches the one from the o outer loop
For all characters beyond the current one
copy the character to the prior array element
decrement size
The indents did not work.. each line is indented one until the last one where it is inside the last for loop
@giblit
That block of code doesn't quite match the algorithm provided.

@jer311
Could you elaborate on that? I completely missed something in that statement. ._.

-Albatross
Last edited on
Algorithm for delete_functions:

1. for each character in the array
a. for each character beyond the one the outer loop is on
i. if the character from the inner loop matches the one from the outer loop
1. for all characters beyond the current one
a. copy the character to the prior array element
2. decrement size
Indents don't work I guess?
@giblit
What happens when you have 'fleeeeece' instead of 'fleece'?

@jer311
I approached the problem this way (slightly different from your algorithm).

For each character in the input string (assuming null-terminated)...
for(int i = 0; inputStr[i] != '\0'; ++i)

If (and for as long as) the next character matches the current character...
while(inputStr[i] == inputStr[i+1])

Start from the next character and go all the way to the end of the string...
for(int j = i+1; inputStr[j] != '\0'; ++j)

and shift the characters over one spot.
inputStr[j] = inputStr[j+1];

Since size in this case is based on the null-terminator anyway, I cut out the middleman and don't track size directly. :)
Last edited on
Basically you want to do this.

1) loop from the first letter in the string until the second to last (*valid) letter.
2)If the current letter is the same as the next letter shift all the letters from that position ( i + 1 ) to the last letter left by one. ( loop from the letter after the current till the second to last )

here is a revised version of the one I posted earlier:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int i = 0;
int duplicates = 0;
while ( i < size - duplicates - 1 )//you should use a while loop since the size will be changing
{
    if( array[i] == array[i+1] )
    {
        for( int j = i + 1; j < size - duplicates - 1; ++j )
        {
            array[j] = array[j+1];
         }
        ++duplicates;
    }
    ++i;
}
size -= duplicates;


*edit yes you are correct booradley I didn't think about more than two characters. Sorry took so long typing lol.
Last edited on
After doing this (the above code) I received a segmentation fault? what does that mean?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

int length(char array[]);
int delete_duplicates(char array[], int size);

int main()
{

	char array[100]
		= "Marry had a little lamb whos fleece was white as snow";
	
	cout << array <<endl;

	int size;
	size = length(array);
	cout << "size = " << size <<endl;

	size = delete_duplicates(array, size);
	cout << "reduced array = " << array <<endl;
	cout << "reduced array size = " << size <<endl;

}

int length(char array[])
{
	int i;
	for (i=0; '\0' !=array[i]; i++)
	{
	
	}
	return i;
}

int delete_duplicates( char array[], int size)
{
int i, j, k;
	i= 0;

	while ( i < size - k - 1)
	{
	  if (array[i] == array[i+1])
	  {
		for ( int j = i + 1; j < size - k - 1; ++j)
		{
		  array[j] = array[j+1];
		}
		++k;
	  }
	  ++i;
	}
 size -= k;
return i;
}
Last edited on
You're running off the end of an array somewhere (i.e. using an index value that exceeds the bounds of the array). Do you have a debugger at your disposal?

I see that k (declared on line 37) is never initialized to anything. That's a problem, too.
Last edited on
got it, thanks!
Topic archived. No new replies allowed.