score:7

Accepted answer
string[] s = found.Split(',').Distinct().ToArray()

score:4

Rewrite the code that builds the result to output it directly.

ie. rewrite this:

for (m = r.Match(site); m.Success; m = m.NextMatch())
{
     found = found + "," + m.Value.Replace(",", "");
}
return found;

To this:

return (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray();

This will return an array. If you still want it back as a string:

return string.Join(", ", (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray());

You may or may not be able to remove the last .ToArray() from the last code there depending on the .NET runtime version. .NET 4.0 string.Join(...) can take an IEnumerable<string>, whereas previous versions requires an array.

score:4

This will return a string of comma seperated values without duplicates:

var result = string.Join(",",
    r.Matches(site)
        .Cast<Match>()
        .Select(m => m.Value.Replace(",", string.Empty))
        .Distinct()
    );

score:4

this could be one possible solution:

var data = new List<string>();
for (m = r.Match(site); m.Success; m = m.NextMatch())
  data.Add(m.Value.Replace(",", ""));
return String.Join(",", data.Distinct().ToArray());

score:0

You can achieve this in a single LINQ query

string strSentence = "aaa,bbb,ccc,aaa,111,111,ccc";
List<string> results = (from w in strSentence.Split(',') select w).Distinct().ToList();

Related Articles