Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trimming duplicate characters with single character in string

This was a interview question - How will you efficiently trim the duplicate characters in string with single character.

Example: suppose this is the input string

"reeeturrrnneedd"

The output should be:

"returned"

I explained it by using splitting the string and loop through the char array, but interviewer does not convinced with the answer said this is not the efficient way.

private void test()
{
    string s = "reeeturrrnneeddryyf";
    StringBuilder sb = new StringBuilder();
    char pRvChar = default(char);
    foreach (var item in s.ToCharArray())
    {                
        if (pRvChar == item)
        {
            continue;
        }
        pRvChar = item;
        sb.Append(pRvChar);
    }

    MessageBox.Show(sb.ToString());
}

Then I thought about Linq to object and use distinct but it will give incorrect output as it remove all duplicate characters and output will be "retund"

Can someone tell me the more efficient way to do it?

like image 664
Neeraj Kumar Gupta Avatar asked May 26 '18 08:05

Neeraj Kumar Gupta


People also ask

How do I find duplicate characters in a string?

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.


1 Answers

Here's a solution with regex:

Regex regex = new Regex( "(.)\\1+" );
string result = regex.Replace( s,"$1" );

I'm not sure, if this is more efficient that your 'for' loop in terms of execution time, but it is more efficient in terms of developer work. And easy to read, at least for people familiar with regex.

like image 197
Heinz Kessler Avatar answered Nov 14 '22 23:11

Heinz Kessler