Removing Duplicates from a List in C# - Page 3
       by kirupa  |  16 November 2006

In the previous page, I started explaining how the code actually works. Let's pick up from where we left off with this page and wrap things up!


if (!uniqueStore.ContainsKey(currValue))
{
uniqueStore.Add(currValue, 0);
finalList.Add(currValue);
}

Inside our foreach loop, the first thing we do is check if the current value - currValue - is already in our dictionary. We can do that by using the dictionary's ContainsKey method. The ContainsKey method returns a true if the value already exists in the dictionary, but it would return a false is the value does not exist.

Notice that I am negating the result from our ContainsKey by placing a ! operator in front of the uniqueStore variable in the if statement. This ensures that, if ContainsKey returns true, our if statement would see the opposite, false, instead. I guess you could say that if our if statement reads "If key is NOT contained in dictionary..."


uniqueStore.Add(currValue, 0);
finalList.Add(currValue);

If currValue is not in our dictionary, then we get to these two lines. The first line adds the current value from our inputList to our uniqueStore dictionary. We are just filling in a 0 for the dictionary's value field, because we are only using the dictionary for its ability to quickly notify us if a duplicate key is about to be added. Retrieving anything from the dictionary is not our intended goal, so you can pass in any integer value you want.

In the second line, I simply add the current value to our finalList. Because we know that, since this value does not already exist in the dictionary, this has to be a unique value, so let's go ahead and add it in to our finalList.


return finalList;

After our foreach loop terminates, all that is left to do is return our finalList value that now contains the unique elements from the List we were presented with originally. All duplicate values have now been removed!


Brief Walkthrough
Let's say that our input is the array I presented in the first page:

   input = { there, here, deer, dear, there, pier, deer, leer }

Initially, our finalList List as well our uniqueStore Dictionary objects are created. We then enter the foreach loop and will stay here until all elements in the input List have been analyzed.

The first value of currValue is going to be the word there. Since our dictionary does not contain the word there as a key, we add this value to the dictionary as well to our finalList.

Similarly, the second word is here, and it too does not exist in either the uniqueStore or finalList objects. This process of checking and adding the words to our uniqueStore and finalList objects continues until we reach the 5 word, the 2nd instance of the word there.

When your currentValue is the word there (again), your uniqueStore.ContainsKey(currValue) statement will return true. After all, there was the first word you added earlier, so you already have the word there located as a key in the dictionary. Therefore, you do not need to add it to our uniqueStore Dictionary and certainly not our finalList List again. The duplicate word was simply skipped over!

Conclusion
There you have it! A small and fast script for removing duplicates from a List of elements. If you used any other type of data structure besides a List, you should be able to modify the code with little effort.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

 

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.