简体   繁体   中英

Efficient way of grouping characters in string c#

I want an efficient way of grouping strings whilst keeping duplicates and order. Something like this

1100110002200   -> 101020

I tried this previously

_case.GroupBy(c => c).Select(g => g.Key)

but I got 102

But this gives me what I want, I just want to optimize it, so I wouldn't have to scour the entire list each time

static List<char> group(string _case)
{
    var groups = new List<char>();
    for (int i = 0; i < _case.Length; i++)
    {
        if (groups.LastOrDefault() != _case[i])
            groups.Add(_case[i]);
    }
    return groups;
}

You could create a method that loops each character and checks the previous character for equality. If they aren't the same, append/yield return the character. This is pretty easy to do with Linq.

public static string Simplify(string str)
{
    return string.Concat(str.Where((c, i) => i == 0 || c != str[i - 1]));
}

Usage:

string simplified = Simplify("1100110002200");
// 101020

In my testing, my method and yours are roughly equal in speed, mine being insignificantly slower after 10 million executions (4260ms vs 4241ms).

However, my method returns the result as a string whereas yours doesn't. If you need to convert your result back to a string (which is likely) then my method is indeed much faster/more efficient (4260ms vs 6569ms).

While I like the elegant solution of rshepp, it turns out that the very basic code can run even 5 times faster than that.

public static string Simplify2(string str)
{
    if (string.IsNullOrEmpty(str)) { return str; }

    StringBuilder sb = new StringBuilder();
    char last = str[0];
    sb.Append(last);

    foreach (char c in str)
    {
        if (last != c)
        {
            sb.Append(c);
            last = c;
        }
    }

    return sb.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM