by kirupa |
16 November 2006
Now that you have an idea of how to
use the removeDuplicates method from the
previous page,
in this page I will explain how to modify it for non-string
values, and then I'll dive right in and explain why the code
works.
If you want to change your removeDuplicates method to
take care of integers, or doubles, or any other type besides a string, you will need to change the type in
the following highlighted areas from string to the type you
plan on using:
- static
List<string>
removeDuplicates(List<string>
inputList)
- {
-
Dictionary<string,
int>
uniqueStore
=
new
Dictionary<string,
int>();
- List<string>
finalList
=
new
List<string>();
-
-
foreach
(string
currValue
in
inputList)
- {
-
if
(!uniqueStore.ContainsKey(currValue))
- {
-
uniqueStore.Add(currValue,
0);
-
finalList.Add(currValue);
- }
- }
-
return
finalList;
- }
If you want to remove duplicate values from a list of
integers, you would change the highlighted string
text to int.
Now that you have seen the code and learned how to use it,
let's go through the code and figure out why it works!
- static
List<string>
removeDuplicates(List<string>
inputList)
- {
-
Dictionary<string,
int>
uniqueStore
=
new
Dictionary<string,
int>();
- List<string>
finalList
=
new
List<string>();
-
-
foreach
(string
currValue
in
inputList)
- {
-
if
(!uniqueStore.ContainsKey(currValue))
- {
-
uniqueStore.Add(currValue,
0);
-
finalList.Add(currValue);
- }
- }
-
return
finalList;
- }
In the above non-grayed out text, I am declaring a new static method called removeDuplicates. It
takes one argument - a List known as inputList - and returns
a List in the end. You know that a List is being returned
because you can see the List<string> text that appears
directly after our static modifier.
- Dictionary<string,
int>
uniqueStore
= new
Dictionary<string,
int>();
I declare a new dictionary variable called uniqueStore.
This variable will store all of the unique values, but more
on that later. Like I explained in my earlier tutorial on
Dictionaries, a dictionary takes two
pieces of data - a key and a value. In our example, the key
will be a string to correspond to the string data held by
our inputList, and the value will be an int. You will see
later that the value doesn't really matter for removing
duplicates in this case!
- List<string>
finalList
= new
List<string>();
To store our final results, I create and initialize a new
List called finalList. At the end, we hope our
finalList contains all of the unique elements from our
inputList earlier!
- foreach
(string
currValue
in
inputList)
- {
- if
(!uniqueStore.ContainsKey(currValue))
- {
-
uniqueStore.Add(currValue,
0);
-
finalList.Add(currValue);
- }
- }
Now, it is time to iterate through our inputList. I could
have used a standard for loop with initializers starting at
0 and ending before the end of the iterated list, but you
can accomplish the same neatly by using a foreach
statement.
In the foreach statement, I store the current element
from inputList into a string called currValue.
Just
for kicks, here is how the above line of code would look
using a standard for loop:
- for
(int
i =
0; i
<
inputList.Count;
i++)
- {
- string
currValue
=
inputList[i];
- if
(!uniqueStore.ContainsKey(currValue))
- {
-
uniqueStore.Add(currValue,
0);
-
finalList.Add(currValue);
- }
- }
I think the foreach approach is much nicer and
compact, but you should use whichever method you are most familiar
with.
Onwards to the
next page!
|