确保数组在 C# 中是连续的

发布于 2024-10-25 20:20:44 字数 357 浏览 2 评论 0原文

我有一个从第三方提供商处获得的整数数组。这些本来是连续的,但由于某种原因它们错过了一个数字(某些东西抛出异常,它被吃掉并且循环继续缺少该索引)。这给我们的系统带来了一些麻烦,我试图确保我们得到的数组确实是连续的。

这些数字从不同的偏移量开始(有时是 1000,有时是 5820,其他是 0),但无论从哪里开始,它都意味着从那里开始。

验证数组是否连续的最快方法是什么?尽管现在看来这是必需的步骤,但我还必须确保验证不会花费太长时间。我目前从第一个索引开始,选取数字并添加一个,并确保下一个索引包含该数字等。

编辑: 系统失败的原因是人们使用系统的方式,它可能并不总是按照最初选择的方式返回代币——说来话长。不幸的是,数据在到达我们的层之前无法进行纠正。

I've got an array of integers we're getting from a third party provider. These are meant to be sequential but for some reason they miss a number (something throws an exception, its eaten and the loop continues missing that index). This causes our system some grief and I'm trying to ensure that the array we're getting is indeed sequential.

The numbers start from varying offsets (sometimes 1000, sometimes 5820, others 0) but whatever the start, its meant to go from there.

What's the fastest method to verify the array is sequential? Even though its a required step it seems now, I also have to make sure it doesn't take too long to verify. I am currently starting at the first index, picking up the number and adding one and making sure the next index contains that etc.

EDIT:
The reason why the system fails is because of the way people use the system it may not always be returning the tokens the way it was picked initially - long story. The data can't be corrected until it gets to our layer unfortunately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-11-01 20:20:44

如果您确定数组已排序并且没有重复项,您可以检查:

array[array.Length - 1] == array[0] + array.Length - 1

If you're sure that the array is sorted and has no duplicates, you can just check:

array[array.Length - 1] == array[0] + array.Length - 1
∞梦里开花 2024-11-01 20:20:44

我认为这里值得解决一个更大的问题: 如果数据不满足您的要求(连续、无间隙),您将做什么?

如果您仍然要处理数据,那么您可能应该投入时间来使您的系统对数据中的间隙或丢失条目更具弹性。

**如果您需要处理数据并且数据必须干净,您应该与供应商合作,确保他们向您发送格式正确的数据。

如果您要跳过处理并报告错误,那么断言无间隙的前提条件可能是正确的方法。在 C# 中,您可以执行许多不同的操作:

  1. 如果数据已排序并且没有重复项,则只需检查 LastValue == FirstValue + ArraySize - 1 是否。
  2. 如果数据未排序但没有重复,只需排序并执行上述操作。
  3. 如果数据未排序、有重复并且您实际上想要检测间隙,我会使用 LINQ。

列表; gaps = Enumerable.Range(array.Min(), array.Length).Except(array).ToList();

或更好(因为高端值可能超出范围):

int minVal = array.Min();
int maxVal = array.Max();
List<int> gaps = Enumerable.Range(minVal, maxVal-minVal+1).Except(array).ToList();

顺便说一下,传递密集、无间隙的整数数组的整个概念对于两方之间的接口来说有点奇怪,除非有一些与它们相关的附加数据。如果没有其他数据,为什么不只发送范围 {min,max} 呢?

I think it's worth addressing the bigger issue here: what are you going to do if the data doesn't meet your requriements (sequential, no gaps)?

If you're still going to process the data, then you should probably invest your time in making your system more resilient to gaps or missing entries in the data.

**If you need to process the data and it must be clean, you should work with the vendor to make sure they send you well-formed data.

If you're going to skip processing and report an error, then asserting the precondition of no gaps may be the way to go. In C# there's a number of different things you could do:

  1. If the data is sorted and has no dups, just check if LastValue == FirstValue + ArraySize - 1.
  2. If the data is not sorted but dup free, just sort it and do the above.
  3. If the data is not sorted, has dups and you actually want to detect the gaps, I would use LINQ.

List<int> gaps = Enumerable.Range(array.Min(), array.Length).Except(array).ToList();

or better yet (since the high-end value may be out of range):

int minVal = array.Min();
int maxVal = array.Max();
List<int> gaps = Enumerable.Range(minVal, maxVal-minVal+1).Except(array).ToList();

By the way, the whole concept of being passed a dense, gapless, array of integers is a bit odd for an interface between two parties, unless there's some additional data that associated with them. If there's no other data, why not just send a range {min,max} instead?

橘香 2024-11-01 20:20:44
for (int i = a.Length - 2; 0 <= i; --i)
{
    if (a[i] >= a[i+1]) return false; // not in sequence
}
return true; // in sequence
for (int i = a.Length - 2; 0 <= i; --i)
{
    if (a[i] >= a[i+1]) return false; // not in sequence
}
return true; // in sequence
清秋悲枫 2024-11-01 20:20:44

如果数组已排序,Gabe 的方式绝对是最快的。如果数组未排序,那么最好对数组进行排序(使用合并/希尔排序(或类似速度的排序)),然后使用 Gabe 的方式。

Gabe's way is definitely the fastest if the array is sorted. If the array is not sorted, then it would probably be best to sort the array (with merge/shell sort (or something of similar speed)) and then use Gabe's way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文