C# 循环遍历数字数组,将连续的数字放在一个数组中,其他所有数字放在另一个数组中

C# 循环遍历数字数组,将连续的数字放在一个数组中,其他所有数字放在另一个数组中,c#,arrays,int,increment,C#,Arrays,Int,Increment,我有一个未排序的数字数组。此数组中的大多数数字是连续的1、2、3、4、5、,。。。但有时候里面有一个数字比其他的都高 int highestNumber = int.MinValue; int secondHighestNumber = int.MinValue; int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, , 9000, 9001 }; int counter = 0; foreach (in

我有一个未排序的数字数组。此数组中的大多数数字是连续的1、2、3、4、5、,。。。但有时候里面有一个数字比其他的都高

int highestNumber = int.MinValue;
int secondHighestNumber = int.MinValue;

int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, , 9000, 9001 };
int counter = 0;

foreach (int number in arr) {
    if (number > highestNumber) {
        secondHighestNumber = highestNumber;
        highestNumber = number;
    }
    else if (number > secondHighestNumber) secondHighestNumber = number;
}
当我运行代码时,它会告诉我9001是最高的数字,9000是第二高的数字。但是我想改变它,这样我得到一个从1到11的所有数字的数组,所有的连续数字只有一个差,第二个数组包含所有较大的数字:1207,1803,9000和9001。 9000和9001不应该在第一个数组中,因为这些数字与其他数字之间的差异太大。所以最终结果应该是这样的:

int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int[] arr2 = new int[] { 1207, 1803, 9000, 9001 };
关于这一目的的一些背景信息: Cient创建文章,每篇文章都有一个文章编号,该编号是通过查找到目前为止文章表中最高的文章编号来创建的,例如20,那么新文章的编号是该编号+1,例如21;因此,每个数字的增量。然而,客户希望能够有时添加文章并输入他希望的文章编号,因此假设他创建的最后一篇文章输入了7000作为文章编号。如果他在那之后再写一篇文章,我必须继续写递增的数字列表。因此,除非客户再次更改下一篇文章的值,否则下一篇新文章的文章编号应该是22,而不是7001


任何帮助都将不胜感激,因为我一直在努力解决这一问题已经有一段时间了

我不确定您是想得到下一个应该使用的号码,还是想要这两套。如果你想要这两套,我会这样做:

int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, 9000, 9001 };

// Sort the array
Array.Sort(arr);

// Start at the beginning and check if the index matches the actual number.
// Computer begins at 0, so fix that
for (int index=0;index<arr.Length; index++)
{
    if (arr[index] != index+1)
    {
        Console.WriteLine("Next ID is " +(index+1));
        break;
    }
}
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 1207, 7, 8, 9, 1803, 10, 11, , 9000, 9001 };
int counter = 0;
List<int> l1 = new List<int>();
List<int> l2 = new List<int>();

foreach (int number in arr) {
    if(l1.Count==0 || l1[l1.Count-1] == (number -1))
        l1.Add(number);
    else
        l2.Add(number);
}

排序,然后从头开始。@ThomasWeller,但代码返回的最高值为9001,次高值为9000。arr.Selecti=>i+1.exceptar.MinDoes该列表是否总是从1开始?如果1不在列表中,是否应该建议1作为下一个数字?@Magali您对Bradley Uffner和ThomasWeller的最后两条评论不在一起。如果它总是从1开始,那么对{10,11,12,100,101,102,500,501,505}的回答将是1,因为它不在列表中。如果你总是增加可能只包含一个数字的最低范围,那么你不会总是从1开始。你可以修改你的方法。为每篇文章存储两个数字,即系统生成的数字:1、2、3、。。。以及客户指定的号码(如果存在)。使用任何适合于每种情况的数字。