Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
So, I'm making a dictionary for which each key can take multiple values(i.e Dictionary <string, List<string>>) My problem is that for each key I want it to have distinct values aka no redundancy in values for a certain key so I need to check if a key contains a value in the list of string
Posted
Updated 6-Dec-15 12:36pm
v3
Comments
PIEBALDconsult 6-Dec-15 18:38pm    
You can use a HashSet rather than a List. And if you want to be sure that no two sets contain the same value, you can have another HashSet outside the Dictionary.
https://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx
Member 12160712 6-Dec-15 20:00pm    
Thank you so much!
BillWoodruff 7-Dec-15 2:53am    
Indeed, using a HashSet may be useful here ... when we know what the OP really wants :)
PIEBALDconsult 7-Dec-15 18:23pm    
I'll tell you want he wants, what he really really wants...
Sergey Alexandrovich Kryukov 6-Dec-15 23:24pm    
What's wrong with just reading MSDN help? Everything is explained quite clearly.
—SA

Assuming your goal is to ensure the each 'Value field of the Key-Value Pairs contained in your Dictionary has no duplicate Values:
C#
// required
// using System.Collections.Generic;

public class DistinctList : List<string>
{
    public new void Add(string value)
    {
        if (this.Contains(value)) return;

        base.Add(value);
    }
}

public class ValueDistinctDictionary : Dictionary<string, DistinctList>
{
    public void Add(string key, string value)
    {
        if (!this.ContainsKey(key))
        {
            base.Add(key, new DistinctList());
        }

        base[key].Add(value);
    }
}

// test in some method
// set break-points, examine 'vDict
ValueDistinctDictionary vDict = new ValueDistinctDictionary();

vDict.Add("hello", "goodbye");
vDict.Add("hello", "hello again");
vDict.Add("hello", "goodbye");

vDict.Add("hello1", "hello");
vDict.Add("hello1", "hello");
vDict.Add("hello1", "hello");
 
Share this answer
 
Comments
PIEBALDconsult 7-Dec-15 18:44pm    
List.Contains probably won't scale well; hence my suggestion of a HashSet.
Also be aware that your DistinctList might not always behave the way it should, due to a lack of polymorphism.
Member 12160712 7-Dec-15 19:12pm    
Yeah, you're right, it didn't work on my code as much as I wanted to and it could get confusing and messy on a bigger scale, hence I think hashset is the easiest and most reasonable answer to my question, like I have been trying for days and making lots of function to check but I never really thought of changing the type list to a Hashset so thank you, it worked perfectly!
PIEBALDconsult 7-Dec-15 20:49pm    
Yeah, Hash Sets are wonderful, unless you need to index into the set. If you need both functionalities then some custom class that wraps both a List and a HashSet should work.
All you need is reading MSDN help. To check up if an element is in an instance of a HashSet advised by PIEBALDconsult, you can use System.Collections.Generic.HashSet<T>.Contains(T):
https://msdn.microsoft.com/en-us/library/bb356440(v=vs.110).aspx[^].

With Dictionary, you can use System.Collections.Generic.Dictionary<Key, Value>.ContainsKey(Key):
https://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx[^],
see also https://msdn.microsoft.com/en-us/library/a63811ah(v=vs.110).aspx[^].

However, in most cases, it is not the most efficient way, because more often you need combine two steps: get the value by key and determine if the key already exists. This is done by the method TryGetValue:
https://msdn.microsoft.com/en-us/library/bb347013(v=vs.110).aspx[^].

That's all. Do read MSDN before asking questions like that.

—SA
 
Share this answer
 
Comments
BillWoodruff 7-Dec-15 2:52am    
Suggest you read the post again: they are not asking about Keys, but about a Value object which is a List of String.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900