我有数字为 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/