Fastest way to find common items across multiple lists in C#

后端 未结 11 1830

Given the following:

List> optionLists;

what would be a quick way to determine the subset of Option objects that a

11条回答
  •  迷失自我
    2021-01-19 03:03

    /// 
        /// The method FindCommonItems, returns a list of all the COMMON ITEMS in the lists contained in the listOfLists.
        /// The method expects lists containing NO DUPLICATE ITEMS.
        /// 
        /// 
        /// 
        /// 
        public static List FindCommonItems(IEnumerable> allSets)
        {
            Dictionary map = new Dictionary();
            int listCount = 0; // Number of lists.
            foreach (IEnumerable currentSet in allSets)
            {
                int itemsCount = currentSet.ToList().Count;
                HashSet uniqueItems = new HashSet();
                bool duplicateItemEncountered = false;
                listCount++;
                foreach (T item in currentSet)
                {
                    if (!uniqueItems.Add(item))
                    {
                        duplicateItemEncountered = true;
                    }                        
                    if (map.ContainsKey(item))
                    {
                        map[item]++;
                    } 
                    else
                    {
                        map.Add(item, 1);
                    }
                }
                if (duplicateItemEncountered)
                {
                    uniqueItems.Clear();
                    List duplicateItems = new List();
                    StringBuilder currentSetItems = new StringBuilder();
                    List currentSetAsList = new List(currentSet);
                    for (int i = 0; i < itemsCount; i++)
                    {
                        T currentItem = currentSetAsList[i];
                        if (!uniqueItems.Add(currentItem))
                        {
                            duplicateItems.Add(currentItem);
                        }
                        currentSetItems.Append(currentItem);
                        if (i < itemsCount - 1)
                        {
                            currentSetItems.Append(", ");
                        }
                    }
                    StringBuilder duplicateItemsNamesEnumeration = new StringBuilder();
                    int j = 0;
                    foreach (T item in duplicateItems)
                    {
                        duplicateItemsNamesEnumeration.Append(item.ToString());
                        if (j < uniqueItems.Count - 1)
                        {
                            duplicateItemsNamesEnumeration.Append(", ");
                        }
                    }
                    throw new Exception("The list " + currentSetItems.ToString() + " contains the following duplicate items: " + duplicateItemsNamesEnumeration.ToString());
                }
            }
            List result= new List();
            foreach (KeyValuePair itemAndItsCount in map)
            {
                if (itemAndItsCount.Value == listCount) // Items whose occurrence count is equal to the number of lists are common to all the lists.
                {
                    result.Add(itemAndItsCount.Key);
                }
            }
    
            return result;
        }
    

提交回复
热议问题