c# - Merge, Union, Intersect C# List of Objects
问题描述
I am trying to solve this problem:
I have multiple array of string with some having duplicate items. I need to come up with a final list that has most items in each list
a1 = Array{"A", "B", "C","D","E","F"};
a2 = Array{"A", "B", "B", "C","D","D","D","E","F"};
a3 = Array{"A", "B", "B", "C","D","D","E","F"};
a4 = Array{"A", "B", "B", "B", "C","D","D","E","F"};
a5 = Array{"A", "B", "B", ","D","D","E","E","F"};
Final result should be:
FinalArray = {"A", "B", "B", "B", "C","D","D","D","E","E""F"};
Max. occurrence each items accounted in the final result.
How can I achieve this?
解决方案
简单的。
var arrays = new[]
{
new[] {"A", "B", "C", "D", "E", "F"},
new[] {"A", "B", "B", "C", "D", "D", "D", "E", "F"},
new[] {"A", "B", "B", "C", "D", "D", "E", "F"},
new[] {"A", "B", "B", "B", "C", "D", "D", "E", "F"},
new[] {"A", "B", "B", "C", "D", "E", "E", "F"},
};
var result =
arrays
.SelectMany(xs => xs.GroupBy(x => x).Select(x => new { x.Key, Count = x.Count() }))
.GroupBy(x => x.Key, x => x.Count)
.Select(x => new { x.Key, Count = x.Max() })
.SelectMany(x => Enumerable.Repeat(x.Key, x.Count))
.ToArray();
这给出了:A, B, B, B, C, D, D, D, E, E, F
推荐阅读
- azure - 如何使用 powershell 从 Azure keyvault 下载 .cer 格式的证书?
- amazon-web-services - 如何创建 AWS 安全组并在不同的 .tf 文件中使用它?
- python - 通过 TCP 安装 Python 包 (pip)
- windows - 无法在 WebStorm 中将行分隔符设置为 LF
- c - C中的排序算法(降序)问题
- firebase - 如何在 Flutter 上为我的电子商务应用添加购物车页面?
- elasticsearch - Elasticsearch 无法启动并且没有日志 centOS
- android - 手机重启后的 WorkManager 行为?
- javascript - 反应本机纸按钮背景颜色
- swift - How to create a set of two types in swift?