Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

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.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 

1 | 2 | 3


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com