0

Normally when I need to have a list of ints/strings/etc. I create a list like:

var list = new List<string> 

And then I create a hashtable that contains all the strings, and I don't insert into the list unless it isn't in the hashtable i.e. to enforce unique items in the list.

Is there a datatype that can satisfy both of these requirements for me?

6
  • So... I don't get it. Do you want a set of key-value pairs in which everything must be unique, or a set of unique values? Commented Sep 19, 2011 at 21:17
  • Can you please explain what the two requirements are? I am a bit slow today. Commented Sep 19, 2011 at 21:18
  • or maybe a Dictionary<string, List<int>>? Commented Sep 19, 2011 at 21:19
  • Aren't keys in a hashtable already unique, for their unique hash code? Commented Sep 19, 2011 at 21:19
  • @Yam Marcovic: but not the values Commented Sep 19, 2011 at 21:19

7 Answers 7

6

There is. Use HashSet:

var set = new HashSet<int>(); set.Add(4); set.Add(4); // there is already such an element in the set, no new elements added 

Keep in mind, though, that it does not guarantee you the order of elements.

Sign up to request clarification or add additional context in comments.

2 Comments

order is not important, just uniqueness. Does it come with a built in way for iterating (unlike the hashtable).
yes; given the sample above: foreach (var elem in set) { Console.WriteLine(elem); }
1

Do you just mean HashSet<string> ?

All elements in a HashSet<T> are unique; the Add() method returns a bool to indicate if a new item was actually added, or whether it was a no-op.

Comments

0

Is there a datatype that can satisfy both of these requirements for me?

No. A hashtable will provide you a direct access to an element given its unique key, whereas in a list you don't need a key and you could definitely have duplicates.

Comments

0

You can use the HashSet<T> data type MSDN. Which will only allow you to have a single copy of each value.

Comments

0

If you are after a set of unique values only (and don't subsequently care about ordering) then you should look at a HashSet<T>

1 Comment

Woops, a little slow by me! Great minds think alike. Just to highlight that the HashSet as opposed to your list/dictionary approach will not allow ordered iteration.
0

Technically, there is System.Collections.Specialized.OrderedDictionary. However, this is an old non-updated (non-generic) class and I would generally recommend avoiding it ;-)

Represents a collection of key/value pairs that are accessible by the key or index.

In practice I would create a minimal wrapper class that exposes the required operations. (I would likely use a HashSet<T> (for existence) and a List<T> (for ordering), although just a single List<T> is far than sufficient for a relatively small n in most cases -- remember Big-O is about limits.)

Happy coding.

Comments

0
HashSet<string> set = new HashSet<string>(); bool inserted = set.Add("Item"); bool insertedDuplicate = set.Add("Item"); inserted.Dump("Result1"); insertedDuplicate.Dump("Result2"); //Result //Result1 = true //Result2 = false 

You can run this in LinqPad to see the functionality and how it works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.