Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for missing number in sequence

Tags:

c#

algorithm

linq

I have an List<int> which contains 1,2,4,7,9 for example.

I have a range from 0 to 10.

Is there a way to determine what numbers are missing in that sequence?

I thought LINQ might provide an option but I can't see one

In the real world my List could contain 100,000 items so performance is key

like image 720
Jon Avatar asked Mar 19 '10 08:03

Jon


People also ask

How do I find missing values in an Excel sequence?

In a blank cell, enter the formula of =IF(A3-A2=1,"","Missing"), and press the Enter key. In this case, we enter the formula in Cell B2. If there is no missing numbers, this formula will return nothing; if missing numbers exist, it will return the text of "Missing" in active cell.


1 Answers

var list = new List<int>(new[] { 1, 2, 4, 7, 9 }); var result = Enumerable.Range(0, 10).Except(list); 
like image 142
Darin Dimitrov Avatar answered Oct 17 '22 22:10

Darin Dimitrov