Connecting Tech Pros Worldwide Help | Site Map

Generic List. Remove duplicate

shapper
Guest
 
Posts: n/a
#1: May 14 '07
Hello,

I have an Enum and a Generic.List(Of Enum)

1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum

How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.

For example, if I add the following items:

1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)

I want to remove the items added in 5 and 6 because they are repeated
and they were added last.

Thanks,

Miguel

OJ
Guest
 
Posts: n/a
#2: May 14 '07

re: Generic List. Remove duplicate


On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
Quote:
Hello,
>
I have an Enum and a Generic.List(Of Enum)
>
1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum
>
How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.
>
For example, if I add the following items:
>
1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)
>
I want to remove the items added in 5 and 6 because they are repeated
and they were added last.
>
Thanks,
>
Miguel
Miguel,
it might make more sense to restrict adding duplicates than to remove
them after adding....

http://msdn2.microsoft.com/en-us/library/ms132319.aspx

Look at the Example paragraph,.

hth,
OJ


=?Utf-8?B?YnVybG8=?=
Guest
 
Posts: n/a
#3: May 15 '07

re: Generic List. Remove duplicate


I agree with OJ. However if there is a reason why you can not do this then a
way to remove duplicates from the list is sort the list with the sort
function and then call the RemoveAll function with a predicate that compares
the previous mode with the current mode shown below:


private static Mode temp;
private static bool RemoveDuplicate(Mode currentMode)
{
bool isDuplicate;
if (temp == currentMode)
{
isDuplicate = false;
}
else
{
isDuplicate = true;
}
temp = currentMode;
return isDuplicate;

}

list.sort();
list.RemoveAll(RemoveDuplicate);


"OJ" wrote:
Quote:
On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
Quote:
Hello,

I have an Enum and a Generic.List(Of Enum)

1 Public Enum Mode
2 Count
3 Day
4 Month
5 End Enum

How can I loop, in the generic list, from the last item that was added
to the first item that was added and remove the duplicated values.

For example, if I add the following items:

1 Dim gl As New Generic.List(Of Mode)
2 gl.Add(Mode.Count)
3 gl.Add(Mode.Month)
4 gl.Add(Mode.Day)
5 gl.Add(Mode.Count)
6 gl.Add(Mode.Day)

I want to remove the items added in 5 and 6 because they are repeated
and they were added last.

Thanks,

Miguel
>
Miguel,
it might make more sense to restrict adding duplicates than to remove
them after adding....
>
http://msdn2.microsoft.com/en-us/library/ms132319.aspx
>
Look at the Example paragraph,.
>
hth,
OJ
>
>
>
Closed Thread