0

This is the text file I have written :

this is the first line
this is the first line
this is the second line
this is the second line
this is the second line
this is the third line
this is the first line

I am trying to remove the adjacent duplicate strings, so the output would be:

this is the first line
this is the second line
this is the third line
this is the first line

This is what I have written so far :

for(int i = 0; i < n; i++)
getline(infile,arr[i]);

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

this is the output I get:

this is the first line
this is the second line
this is the second line
this is the third line
this is the first line

How to fix this? P.S.: It has to be solved iteratively, which is why I'm trying to do it this way.

8
  • What is the type of arr? Commented Feb 18, 2017 at 7:43
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Commented Feb 18, 2017 at 7:44
  • it is a string array. everything has to be solved iteratively, i cant use any pre-existing functions, or recursion. :{ i am fairly new to computer science and really struggling with this problem. Commented Feb 18, 2017 at 7:49
  • by debugger do you mean compiler ? ive worked on it for a while now, i thought i could really use some help. Commented Feb 18, 2017 at 7:51
  • Your problem happens when you need to deduplicate the string more than 2 times, so in your case it happened because "second line" is repeated 3 times. Try to trace your code by hand on a piece of paper and see why it's not working correctly. Commented Feb 18, 2017 at 7:54

3 Answers 3

2

Your problem happens when you have more than 2 duplicates of the same line. In your case, this causes the problem.

this is the second line
this is the second line
this is the second line

The issue is that you only de-duplicate the line once with the next element.

Example: If you have 4 lines in which lines 1, 2, 3 are duplicates, you would:

  1. Compare 1 with 2, and then remove 2, compressing the array to 1, 3

  2. Instead of comparing 1 with 3, you would skip to compare 3 with 4 (wrong)

In order to fix your solution, you need to make sure you don't increment i until it doesn't match the next element i+1.

i = 0;
while (i < n)
{
    int j = i+1;
    if(arr[i] == arr[j])
    {
        for(int k = i; k<n; k++)
        {
            arr[k] = arr[k+1];
        }
        n--;
    }
    else
    {
        i++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use std::unique.

auto end = std::unique(std::begin(arr), std::end(arr));

1 Comment

sorry, i should have mentioned, i cant use this. i have to do it iteratively. which is why im trying to it this way, i cant use recursion either.
0
for(int i=0; i < count(original_array); i++)
{
    if(i == 0) // check for first entry
    {
        new_array[] = original_array[i];
        temp = original_array[i];
    }
    if(temp != original_array[i]) // check thereafter
    {
        new_array[] = original_array[i];
        temp = original_array[i];
    }
}

2 Comments

new_array[] = original_array[i]; This won't compile at all.
Hii Friend,in that line, i just wanted to tell that, put the old value to new array.... the code is not written for any particular language... it's just a Logic...

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.