0

what is your simplest algorithm to find duplicate chars?

string a = "school";
string b = "ofrock";

output should be o,c (not ooc). can you find O(n) linear complexity? I can't

string a = "School";
string b = "ofRock";
string c = a + b;
char[] cc = c.ToCharArray();

Dictionary<char, int> d = new Dictionary<char, int>();
Dictionary<char, int> l = new Dictionary<char, int>();

foreach (char ccc in cc)
{
    try
    {
        d.Add(ccc, 1);
    }
    catch
    {
        try
        {
            l.Add(ccc, 1);
        }
        catch
        {
        }
    }
}
4

1 回答 1

3

简单的方法,您可以使用Intersect

var result = a.Intersect(b);
于 2013-09-06T02:35:08.703 回答