Connecting Tech Pros Worldwide Help | Site Map

Generic List. Remove duplicate

  #1  
Old May 14th, 2007, 02:45 PM
shapper
Guest
 
Posts: n/a
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

  #2  
Old May 14th, 2007, 04:05 PM
OJ
Guest
 
Posts: n/a

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


  #3  
Old May 15th, 2007, 05:55 AM
=?Utf-8?B?YnVybG8=?=
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List dmp answers 8 January 8th, 2008 10:15 PM
Removing reference type members from a generic list clone =?Utf-8?B?Sm9lbCBNZXJr?= answers 7 November 28th, 2007 05:15 PM
Generic List Order shapper answers 0 May 10th, 2007 08:55 PM
Generic List object Sehboo answers 7 February 15th, 2007 09:05 PM