VS2008 C#: most optimum way to remove multiple occurrences
Lets say I have a collection of words such as follows:
{"hello", "world", "my", "hello", "world", "sam", "world"}
I would like to remove any multiples such that result would be like the following after parsing.
{"hello", "world", "my", "sam"}
How can开发者_Go百科 I do that in most optimized way.
I don't know about most optimum, but System.Linq.Enumerable.Distinct is certainly the most concise way.
// using System.Linq;
string[] words = {"hello", "world", "my", "hello", "world", "sam", "world"};
var uniqueWords = words.Distinct();
If you're using .NET 3.5, you can insert them into a HashSet<T>
, then (if you want the order maintained) go through your original list and add the item that's in the hashset. This will be O(n), as it does it in a single pass
string[] values = new[] {"hello", "world", "my", "hello", "world", "sam", "world"};
HashSet<string> hashSet = new HashSet<string>();
List<string> newValues = new List<string>(); // or LinkedList<string>, if you don't want the cost of backing array resizes
foreach (string val in values) {
if (!hashSet.Contains(val)) {
newValues.Add(val);
hashSet.Add(val);
}
}
// newValues is the result you want
If it's .NET 2.0, you get the same performance using a Dictionary<string, object>
instead of HashSet<T>
, with null
as the values
The constructor for the HashSet will filter the list for you.
var distinctItems = new HashSet<string>((IEnumerable<string>)myItems);
There are definitely more efficient ways then this, I'm just a Linq fan ;)
IEnumerable<string> reducedList =
originalList.GroupBy(s => s).Select(s => s.First());
List<string> myStrings = new List<string>(){"hello", "world", "my", "hello", "world", "sam", "world"};
var b = ((from a in my myStrings select a).Distinct()).ToList();
精彩评论