By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Generic List. Remove duplicate

P: 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

May 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
OJ
On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
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
May 14 '07 #2

P: n/a
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:
On 14 May, 14:42, shapper <mdmo...@gmail.comwrote:
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
May 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.