课程: C# Practice: Generic Collections

Solution: HashSet operations - C#教程

课程: C# Practice: Generic Collections

Solution: HashSet operations

- [Narrator] One of the reasons I created this code challenge is because I wanted you to work with the hash set. I think it's one of the underused types in the generic collections in .net. It's fast, it's efficient, it only supports unique values. If you attempt to add a duplicate value to a hash set, then nothing happens. It's just ignored. There's no exception thrown. It also has very fast set operations. So while the code challenge itself was to create this word search game, that's not what we're really doing here. Is we're just working with the hash set with a set of words in it, and we're modifying the code in these two methods to get player valid words method and the remove duplicate words method. And in both these cases they take a hash set, or multiple hash sets as parameters, and return a hash set of string. In my comments I mentioned you might want to consider using some of the hash set methods, like intersect with, or accept with, or union with. There's also link equivalents. The differences are listed here. The hash set methods are immutable, meaning that the original hash set is changed. Whereas the link methods are immutable, meaning that you get back a new hash set. So in my example from my code challenge and my solution, I chose the link methods. But the syntax is nearly identical. So let's see how I solved this challenge. So for the first one, get player valid words. We're going to check this valid words hash set against the player's words. And the way I solved this was by using the intersect method. So what this does is to perform a set operation that returns the common elements between the two collections. So it'll return all the items that match. So if the player passes in this word like F F F or D D D, that's not valid. So that's not going to be in both of the hash sets. So that will eliminate these wrong words, or these incorrect, or non-valid words. And then I'm returning this. Now by default, this returns an IEnumerable of string. So since I'm returning a hash set of string here I call to hash set. For the remove duplicate words you get two hash sets passed in. And what we're going to do is remove the duplicate words that are in both hash sets. So for this example, I chose to use the except link method. So you take the first set, call except, pass it the second set, and then again call to hash set to get a hash set back. And it tells you here the except method is used to perform a set operation that returns the elements from one collection that are not present in another collection. And it says here, it's commonly used to find the differences between the two sequences. So once that's done, the rest of the code runs. And we see over here that player one has the high score. You can see the validated contents. These are the words that were correct in player one and player two. Here's the unique words. And then we see that player one had three words, whereas player two only had one word.

内容