C#: Remove duplicate values from dictionary?

前端 未结 8 2513
小蘑菇
小蘑菇 2020-12-09 03:28

How can I create a dictionary with no duplicate values from a dictionary that may have duplicate values?

IDictionary myDict = new Dicti         


        
相关标签:
8条回答
  • 2020-12-09 04:00

    The brute-force solution would be something like the following

    var result = dictionary
        .GroupBy(kvp => kvp.Value)
        .ToDictionary(grp => grp.First().Value, grp.Key)
    

    assuming you don't really care about the key used to represent a group of duplicates and it is acceptable to rebuild the dictionary.

    0 讨论(0)
  • 2020-12-09 04:06

    Just a footnote to those using the Revit API, this is one method that works for me in removing duplicate elements, when you can't use say wallType as your object type and instead need to leverage raw elements. it's a beaut mate.

      //Add Pair.value to known values HashSet
                     HashSet<string> knownValues = new HashSet<string>();
    
                    Dictionary<Wall, string> uniqueValues = new Dictionary<Wall, string>();
    
                     foreach (var pair in wall_Dict)
                     {
                         if (knownValues.Add(pair.Value))
                         {
                             uniqueValues.Add(pair.Key, pair.Value);
                         }
                     }
    
    0 讨论(0)
  • 2020-12-09 04:08
    foreach (var key in mydict.Keys)
      tempdict[mydict[key]] = key;
    foreach (var value in tempdict.Keys)
      uniquedict[tempdict[value]] = value;
    
    0 讨论(0)
  • 2020-12-09 04:11

    Jon beat me to the .NET 3.5 solution, but this should work if you need a .NET 2.0 solution:

            List<string> vals = new List<string>();
            Dictionary<string, string> newDict = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> item in myDict)
            {
                if (!vals.Contains(item.Value))
                {
                    newDict.Add(item.Key, item.Value);
                    vals.Add(item.Value);
                }
            }
    
    0 讨论(0)
  • 2020-12-09 04:13

    In addition to the answer of Jon Skeet , if your value is an intern object you can use :

    var uniqueValues = myDict.GroupBy(pair => pair.Value.Property)
                         .Select(group => group.First())
                         .ToDictionary(pair => pair.Key, pair => pair.Value);
    

    This way you will remove the duplicate only on one property of the object

    0 讨论(0)
  • 2020-12-09 04:16
    Dictionary<string, string> test = new Dictionary<string,string>();
    test.Add("1", "blue");
    test.Add("2", "blue");
    test.Add("3", "green");
    test.Add("4", "red");
    Dictionary<string, string> test2 = new Dictionary<string, string>();
    foreach (KeyValuePair<string, string> entry in test)
    {
        if (!test2.ContainsValue(entry.Value))
            test2.Add(entry.Key, entry.Value);
    }
    
    0 讨论(0)
提交回复
热议问题