c# - 查找数字数组中丢失的元素

标签 c# .net arrays

我有数字为 1...31 的数组(一个月中的天数) 就我而言,我想找到所有丢失的号码。 这是我的解决方案,但我不确定这是一个好方法。

var result = new List<int>();
int[] days = GetSelectedDays(); //I recive array with 1..31 with some missing elements sometimes (without "5" as example it depends if user has selected this number)
for (int i=0; i <30; i++)
{
  if (!days.Contains(i))
  result.Add(i);
}

最佳答案

您可以使用 LINQ 异常(exception):

var result = Enumerable.Range(1, 31).Except(days);

关于c# - 查找数字数组中丢失的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399788/

相关文章:

c# - 执行多个查询时如何保持连接打开?

c# - 删除多元素string []数组中的重复项?

.net - 为什么在提供阿拉伯文本时 String.IndexOf 和 String.Contains 不一致?

c++ - C++类中的指针数组

javascript - 按属性对数组中的元素进行分组

java - 将数组更改为 ArrayList

c# - mysql 中可选的 where 子句条件

c# - 如何处理 C# 字符串插值中的空值?

C# VB.NET : How to make a jagged string array a public property

C# - 是否可以隐藏公开继承的方法(例如,对派生类私有(private))