0

I have a string:

string  input = "aaaabbcccghbcccciippppkkllk"

The output should be:

abcghbcipklk

Note: Must use loop or recursion. etc.

Just want to know is there better and efficient code than this:

    string input = "aaaabbcccghbcccciippppkkllk";
    int j=0;

    for (int i=0; i<input.Length - 1; i++) {

        for(j=i+1; j<input.Length && input[i] == input[j]; j++);

        input = input.Substring(0, i+1) + input.Substring(j); 
    }
    Console.WriteLine(input);
Rehan Ali Khan
  • 527
  • 10
  • 23

3 Answers3

5

According to my understanding, you want to eliminate duplicates only if it is in a consecutive sequence. You could achieve it using the following

Using List<string>

var nonDuplicates = new List<char>();

foreach (var element in str.ToCharArray())
{
    if(nonDuplicates.Count == 0 || nonDuplicates.Last() != element)
        nonDuplicates.Add(element);
}

var result = new string(nonDuplicates.ToArray());

Update

With reference to comment from , I have updated and the answer with two more solutions and ran the benchmark on them. The results are shown below.

Using String Append

 var str = "aaaabbcccghbcccciippppkkllk";
  var strResult = string.Empty;

  foreach (var element in str.ToCharArray())
  {
     if (strResult.Length == 0 || strResult[strResult.Length - 1] != element)
        strResult = $"{strResult}{element}";
  }

Using StringBuilder

  var str = "aaaabbcccghbcccciippppkkllk";
  var strResult = new StringBuilder();

  foreach (var element in str.ToCharArray())
  {
     if (strResult.Length == 0 || strResult[strResult.Length - 1] != element)
       strResult.Append(element);
  }
  var result = strResult.ToString();

Benchmark Results

             Method |       Mean |     Error |     StdDev |     Median |
------------------- |-----------:|----------:|-----------:|-----------:|
          UsingList |   809.7 ns | 11.975 ns |  11.202 ns |   806.5 ns |
  UsingStringAppend | 1,738.0 ns | 39.269 ns | 109.467 ns | 1,697.2 ns |
 UsingStringBuilder |   201.6 ns |  1.960 ns |   1.834 ns |   201.1 ns |

As seen in the results, the StrinbBuilder Approach is much fast when compared to List. The string append approach is slowest.

Input

aaaabbcccghbcccciippppkkllk

Output

abcghbcipklk
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
2

Ok, here is how you can do that using "simple" language:

string input = "aaaabbcccghbcccciippppkkllk"

var result = "" + input[0];
for(var i = 1; i < input.Length; i++)
{
    if(input[i] != result[result.Length-1])
    {
        result += input[i];
    }
}
Rehan Ali Khan
  • 527
  • 10
  • 23
Roman.Pavelko
  • 1,555
  • 2
  • 15
  • 18
0
string str = "abbxxcccxbaac";
string nstr = str[0].ToString();
               

for(int j = 0; j < str.Length - 1; j++)
{
    if(!str[j].Equals(str[j + 1]))
    {
        nstr = nstr + str[j + 1];
    }
}

It will remove sequential char from the string. nstr is output.

  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Gerhard Jul 12 '21 at 07:42