首页 > 解决方案 > 查找项目列表中出现频率最高的组合

问题描述

在我的 asp.net c# 应用程序中,我有以下项目组合的出现列表。我想列出最常出现的组合。

  1. 项目1
  2. 项目 1、项目 2
  3. 第 3 项
  4. 项目 1、项目 3、项目 2
  5. 第 3 项,第 1 项
  6. 第 2 项,第 1 项

根据上面的例子,我应该得到下面的输出。

最常出现的组合是;

  1. 项目 1 和项目 2 - 出现次数为 3(#2、#4 和 #6)
  2. Item1 & Item3 - 出现次数为 2 (#4 & #5)

我的结构如下。

public class MyList
{
    public List<MyItem> MyItems { get; set; }
}

public class MyItem
{
    public string ItemName { get; set; }
}

标签: c#combinations

解决方案


Out of the top of my head i would map all possible combinations using a hash where ab is the same as ba (or you could order your items alphabetically for example and then hash them) and then just count occurrences of the hashes...


推荐阅读