2

I've always thought the any index should be unique, but I think it's not true at least for SQL Server as shown in the following post:

Do clustered indexes have to be unique?

Recently I had to store a very amount of data within a collection and thought of using a dictionary for it's the fastest collection to get an object by index. But my collection would have to allow duplicated keys. But in fact duplicated keys would not be a problem since any of the object returned would be meet the requirements (The objects are not exactly unique, but the keys would be).

Some more research led me to the following post:

C# Hashset Contains Non-Unique Objects

Which shows a way to get a HashSet with "duplicated keys". His problem would be my solution but I wonder if there's any other way that I can have a list with duplicated keys which allows me to search very fast without having to do any workaround the get this done.

Community
  • 1
  • 1
Androiderson
  • 16,865
  • 6
  • 62
  • 72
  • If you have duplicated keys how are you going to get objects out of the list using the key as there could be more than 1, or are you planning on returning many items if there is more than 1 with the same index??? And as far as I know no Dictionary or List implementation allows duplicate keys, you would have to build one from scratch, that being said why even use a key as if there is duplicates its redundent. – sa_ddam213 Jan 22 '13 at 01:54
  • I'm glad you're referencing other questions, but please edit your question so that I don't have to read the other questions in order to understand yours. – John Saunders Jan 22 '13 at 01:54
  • @JohnSaunders thanks for the tip, I hope it's a bit more clear now. I'll try to improve future posts. – Androiderson Jan 22 '13 at 02:13

1 Answers1

0

"duplicated indexes would not be a problem since any of them would be meet the requirements"

If by this, you mean that obtaining any item stored against the same index value would be satisfactory you when retrieving an item by index, then a simple Dictionary will suffice.

E.g.

Dictionary<int, string> myData = new Dictionary<int, string>();

myData[1] = "foo";
myData[2] = "bar";
myData[2] = "baz"; // overwrites "bar"

var myDatum = myData[2]; // retrievs "baz" not "bar", but this is satisfactory.
Ergwun
  • 12,579
  • 7
  • 56
  • 83