Remove duplicates in multi-element string[] array?
I have a string[] array that is split by \r
. Each row in the array has title|address in it, but every so often I end up with a duplicate of the address portion of it, which I don't want.
This:
Title1 | Address1 //[0]
Title2 | Address2 //[1]
Title3 | Address1 //[2]
Title4 | Address3 //[3]
Would become:
Title1 | Address1 //[0]
Title2 | Address2 //[1]
Title4 | Address3 //[2]
The array declaration is as follows: string[] resultsArray = results.Split('\r'); //Title|Address
I then later split the row when I grab the individual elements by |
.
Usage (extre开发者_如何学Gomely simplified):
foreach (string result in resultsArray)
{
string splitResult[] = result.Split('|');
title = splitResult[0];
address = splitResult[1];
}
I'm assuming the question is, how do you prevent duplicate addresses from being entered into the list. Could you use a Dictionary?
Dictionary<string, string> addresses = new Dictionary<string, string>();
foreach(string result in resultsArray)
{
string splitResult[] = result.Split('|');
// check to see if address already exists, if it does, skip it.
if(!addresses.ContainsKey(splitResult[1]))
{
addresses.add(splitResult[1], splitResult[0]);
}
}
string[] strings = { "Title1 | Address1", "Title2 | Address2", "Title3 | Address1", "Title4 | Address3" };
var _strings = strings.GroupBy(s => s.Split('|')[1]).Select(g => g.Min(s => s));
var seenItBefore = new HashSet<string>();
foreach (string result in resultsArray)
{
string splitResult[] = result.Split('|');
title = splitResult[0];
address = splitResult[1];
if (!seenItBefore.Add(address)) continue;
// process
}
You could also supply a projecting IEqualityComparer<string[]>
to Distinct()
if you're building up a vine of IEnumerable<>
, but since your sample doesn't use it, I decided to stick with classic procedural.
Using John Skeet's ProjectionComparer
, it becomes rather easy:
var comparer = new ProjectionComparer((string input) => input.Split('|')[1]);
var results = resultsArray.Distinct(comparer);
Try something like:
resultArray.Select(p => p.Split('|')).Select(p => new { Name = p[0], Address = p[1] }).GroupBy(p => p.Address).Select(p => p.First()).ToArray();
精彩评论