473,414 Members | 1,575 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,414 software developers and data experts.

List with no duplicates

Hello, Newsgroupians:

Just a quick question really quick. Does C# have a generic class that will
allow me to add only one instance of an object to the "collection" without
having me do a .Contains() or something similar?

In my case, I have a set of controls a user selects. If the user selects
the same control twice, I don't want it added again to my collection, and I
don't want to handle the exception, and I think .Contains() is too expensive.
Does anyone have any recommendations? I've tried a Dictionary with the key
being the control as well as the value being the control, but again doing
this led to an exception. Is there any other way or generic class that I
should look at?

Thank you.
Trecius
Nov 6 '07 #1
8 37239
Trecius,

No, in .NET 3.0 and before, you have to do this yourself with a check to
Contains.

In .NET 3.5, you would use the new HashSet class.

Before that, you can use a Dictionary<TKey, TValueclass. The type
parameter TValue doesn't really matter here. It's TKey that you are
concerned about. The TKey type parameter should be set to the type of the
instances you don't want to duplicate. You can use bool for the TValue, if
you wish.

Then, before you add to the Dictionary, call Contains. If Contains
returns true, then the item exists.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hello, Newsgroupians:

Just a quick question really quick. Does C# have a generic class that
will
allow me to add only one instance of an object to the "collection" without
having me do a .Contains() or something similar?

In my case, I have a set of controls a user selects. If the user selects
the same control twice, I don't want it added again to my collection, and
I
don't want to handle the exception, and I think .Contains() is too
expensive.
Does anyone have any recommendations? I've tried a Dictionary with the
key
being the control as well as the value being the control, but again doing
this led to an exception. Is there any other way or generic class that I
should look at?

Thank you.
Trecius

Nov 6 '07 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Trecius,

No, in .NET 3.0 and before, you have to do this yourself with a check to
Contains.

In .NET 3.5, you would use the new HashSet class.

Before that, you can use a Dictionary<TKey, TValueclass. The type
parameter TValue doesn't really matter here. It's TKey that you are
concerned about. The TKey type parameter should be set to the type of the
instances you don't want to duplicate. You can use bool for the TValue, if
you wish.

Then, before you add to the Dictionary, call Contains. If Contains
returns true, then the item exists.
It should also be noted that with dictionaries, Contains is really not
an expensive call. It could be if the number of items was really large,
but sounds like the OP is talking about user controls that a user would
select, something that isn't likely to come anywhere near having enough
to cause any performance issues with a call to Contains.
--
Tom Porterfield
Nov 6 '07 #3
On 2007-11-06 13:18:32 -0800, Tom Porterfield <tp******@mvps.orgsaid:
[...]
Then, before you add to the Dictionary, call Contains. If
Contains returns true, then >the item exists.

It should also be noted that with dictionaries, Contains is really not
an expensive call. It could be if the number of items was really
large, but sounds like the OP is talking about user controls that a
user would select, something that isn't likely to come anywhere near
having enough to cause any performance issues with a call to Contains.
I would reverse that. Using a Dictionary<>, the call should not be
expensive regardless, even if the number of elements is large.
Assuming this is a case of dealing with user controls, a plain old tree
traversal is likely to be plenty fast as long as it's not the primary
thing that the program is doing (and it's hard to imagine why it would
be).

Pete

Nov 7 '07 #4
On Nov 6, 4:05 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Trecius,

No, in .NET 3.0 and before, you have to do this yourself with a check to
Contains.

In .NET 3.5, you would use the new HashSet class.

Before that, you can use a Dictionary<TKey, TValueclass. The type
parameter TValue doesn't really matter here. It's TKey that you are
concerned about. The TKey type parameter should be set to the type of the
instances you don't want to duplicate. You can use bool for the TValue, if
you wish.

Then, before you add to the Dictionary, call Contains. If Contains
returns true, then the item exists.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Trecius" <Trec...@discussions.microsoft.comwrote in message

news:BA**********************************@microsof t.com...
Hello, Newsgroupians:
Just a quick question really quick. Does C# have a generic class that
will
allow me to add only one instance of an object to the "collection" without
having me do a .Contains() or something similar?
In my case, I have a set of controls a user selects. If the user selects
the same control twice, I don't want it added again to my collection, and
I
don't want to handle the exception, and I think .Contains() is too
expensive.
Does anyone have any recommendations? I've tried a Dictionary with the
key
being the control as well as the value being the control, but again doing
this led to an exception. Is there any other way or generic class that I
should look at?
Thank you.
Trecius- Hide quoted text -

- Show quoted text -
Do you really have to call Contains before updating a Dictionary ?

I mean, I thought that a Dictionary could not _inherently_ contain
multiple entries, because after all it is just a Key-Value pair, not a
Key-ListOfValues pair ?

EG,
The code below should not except and should not store any duplicates!

Dictionary[ objectA.UniqueID ] = "specialValue";
Dictionary[ objectA.UniqueID ] = "anotherSpecialValue"

Right ?

Nov 7 '07 #5
On Nov 6, 3:46 pm, Trecius <Trec...@discussions.microsoft.comwrote:
Hello, Newsgroupians:

Just a quick question really quick. Does C# have a generic class that will
allow me to add only one instance of an object to the "collection" without
having me do a .Contains() or something similar?

In my case, I have a set of controls a user selects. If the user selects
the same control twice, I don't want it added again to my collection, and I
don't want to handle the exception, and I think .Contains() is too expensive.
Does anyone have any recommendations? I've tried a Dictionary with the key
being the control as well as the value being the control, but again doing
this led to an exception. Is there any other way or generic class that I
should look at?

Thank you.

Trecius
My solution is outside of the Key-Value box :)

Since we want the list to contain unique values, only store those
values as the KEYS in the Dictionary. The dictionary , by definition,
does not store more than one value per key. You can then copy the
Dictionary.Keys property into a list and rest assured that there are
no duplicates.

Nov 7 '07 #6
On Nov 7, 12:58 pm, Norapinephrine <N.AhmedRo...@gmail.comwrote:
On Nov 6, 3:46 pm, Trecius <Trec...@discussions.microsoft.comwrote:


Hello, Newsgroupians:
Just a quick question really quick. Does C# have a generic class that will
allow me to add only one instance of an object to the "collection" without
having me do a .Contains() or something similar?
In my case, I have a set of controls a user selects. If the user selects
the same control twice, I don't want it added again to my collection, and I
don't want to handle the exception, and I think .Contains() is too expensive.
Does anyone have any recommendations? I've tried a Dictionary with the key
being the control as well as the value being the control, but again doing
this led to an exception. Is there any other way or generic class that I
should look at?
Thank you.
Trecius

My solution is outside of the Key-Value box :)

Since we want the list to contain unique values, only store those
values as the KEYS in the Dictionary. The dictionary , by definition,
does not store more than one value per key. You can then copy the
Dictionary.Keys property into a list and rest assured that there are
no duplicates.- Hide quoted text -

- Show quoted text -
Let me expand :

object inputValue = GetUserSelectedValueFromGUI();

dictionary[inputValue] = 99; // the only thing important here is that
we stored inputValue as a KEY in the dictionary.

....

List<objectnoDuplicatesListOfUserInput = dictionary.Keys;

Hope this helps!

Nov 7 '07 #7
On 2007-11-07 09:55:53 -0800, Norapinephrine <N.**********@gmail.comsaid:
[...]
Do you really have to call Contains before updating a Dictionary ?
No, you're right. You can catch an exception instead when calling
Add(), or you can use the Item property as you've suggested.
I mean, I thought that a Dictionary could not _inherently_ contain
multiple entries, because after all it is just a Key-Value pair, not a
Key-ListOfValues pair ?
The keys must be unique, yes.
EG,
The code below should not except and should not store any duplicates!

Dictionary[ objectA.UniqueID ] = "specialValue";
Dictionary[ objectA.UniqueID ] = "anotherSpecialValue"

Right ?
Yes. If the key doesn't already exist, a set operation on the Item
property will create a new entry in the Dictionary with the specified
value. If the key does already exist, the value associated with the
key will be replaced by the specified value.

Pete

Nov 7 '07 #8
Just have:

Hashtable ht = new Hashtable ();

Then use:
this.ht [key] = null;

Using "= 99" as you did raises boxing issues and could create a huge
performance hit.

Hilton
Nov 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an...
3
by: Mark V. | last post by:
Here's what I have and I'm stumped. I have a table that has several thousand names and addresses. I only want to send to one address in a household. So what I would like to do is create a new...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
2
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something like this: unid | oid ---------+---------
4
by: Killer42 | last post by:
Hi all. Sorry, this is probably a really simple one but I'm having some difficulty with it, and don't have much time to devote to it right now. I need to find all the records which have...
2
by: kalar | last post by:
I have an exercise which says: create a data type called total integers (double(same) values are not allowed) How will i do this? I have already a function removeduplicates from another program...
1
by: AndyB | last post by:
I have found a lot of material on removing duplicates from a list, but I am trying to find the most efficient way to just check for the existence of duplicates in a list. Here is the best I have...
22
by: Simon Forman | last post by:
Is there a more efficient way to do this? def f(L): '''Return a set of the items that occur more than once in L.''' L = list(L) for item in set(L): L.remove(item) return set(L)
3
by: Mike Copeland | last post by:
How do I work with a std::list that might have multiple objects having the same "key", but which have other data that is different? Here's code that compiles, but doesn't do quite what I expect:...
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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.