473,413 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Generic List. Remove duplicate

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
2 12294
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shi Mu | last post by:
I used the following method to remove duplicate items in a list and got confused by the error. >>> a , , ] >>> noDups='] ] Traceback (most recent call last): File "<interactive input>", line...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
6
by: junky_fellow | last post by:
Hi all, I have a linked list in which each node is of follwoing type. struct node { struct node *fptr; int data; int flag; /* flag could be ORIG=1 or DUPLICATE=2 */ }
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
0
by: shapper | last post by:
Hello, I have an enum as follows: Public Enum Feature Title Content Date Search End
3
by: Doug | last post by:
I have a generic list object with a property called, "MarkedForDeletion". During the course of my processing, some of the objects in the list will get this property set to true and so at the end...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
1
by: DNB | last post by:
I am using generic in the following example. But I can figure out how to remove an item in the collection?? Thanks DNB
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.