Get the next available integer using LINQ

Say I have a list of integers:

List<int> myInts = new List<int>() {1,2,3,5,8,13,21};

I would like to get the next available integer ordered by increasing the integer. Not the last or highest, but in this case, the next integer that is not on this list. In this case, the number is 4.

Is there a LINQ instruction that will give me this? How in:

var nextAvailable = myInts.SomeCoolLinqMethod();

Edit: Shit. I said that the answer should be 2, but I had in mind 4. I apologize for this!

For example: Imagine that you are responsible for issuing process identifiers. You want to get a list of current process identifiers and issue the next, but the next should not be the highest value plus one. Rather, it should be next, accessible from an ordered list of process identifiers. You can get the next available launch with the highest, it doesn't really matter.

+5
source share
7 answers
public static class IntExtensions
{
    public static int? SomeCoolLinqMethod(this IEnumerable<int> ints)
    {
        int counter = ints.Count() > 0 ? ints.First() : -1;

        while (counter < int.MaxValue)
        {
            if (!ints.Contains(++counter)) return counter;
        }

        return null;
    }
}

Using:

var nextAvailable = myInts.SomeCoolLinqMethod();
0
source

I see a lot of answers that write their own extension method, but you can solve this problem with the standard linq extension methods and the static Enumerable class:

List<int> myInts = new List<int>() {1,2,3,5,8,13,21};

// This will set firstAvailable to 4.
int firstAvailable = Enumerable.Range(1, Int32.MaxValue).Except(myInts).First();
+26
source

, @Kevin, . : .Count, .FirstOrDefault .Contains. IEnumerable<int> , .Select, 2 , . , . , { 1, 1000000 }, , .

LINQ . . , . , , 1 , 1 :

public static int? FirstMissing(this IEnumerable<int> numbers)
{
    int? priorNumber = null;

    foreach(var number in numbers.OrderBy(n => n))
    {
        var difference = number - priorNumber;

        if(difference != null && difference > 1)
        {
            return priorNumber + 1;
        }

        priorNumber = number;
    }

    return priorNumber == null ? (int?) null : priorNumber + 1;
}

, . . , priorNumber , , difference null. , , 1. , , , 1 .

return 0 1 , ; null n + 1 { n }.

+4

:

static int Next(this IEnumerable<int> source)
{
    int? last = null;
    foreach (var next in source.OrderBy(_ => _))
    {
        if (last.HasValue && last.Value + 1 != next)
        {
            return last.Value + 1;
        }

        last = next;
    }

    return last.HasValue ? last.Value + 1 : Int32.MaxValue;
}
+2

, , , .

var nextAvailableInteger = Enumerable.Range(myInts.Min(),myInts.Max()).FirstOrDefault( r=> !myInts.Contains(r));

- , . , .

: , , . - LINQ , . , .

public static int NextAvailableInteger(this IEnumerable<int> ints)
{
    return NextAvailableInteger(ints, 1); // by default we use one
}
public static int NextAvailableInteger(this IEnumerable<int> ints, int defaultValue)
{
    if (ints == null || ints.Count() == 0) return defaultValue;

    var ordered = ints.OrderBy(v => v);
    int counter = ints.Min();
    int max = ints.Max();

    while (counter < max)
    {
        if (!ordered.Contains(++counter)) return counter;
    }
    return (++counter);
}
0

, Linq, SO-

var thelist = new List<int> {1,2,3,4,5,100,101};

var nextAvailable = (from curr in thelist
                    join next in thelist
                        on curr + 1 equals next into g
                    from newlist in g.DefaultIfEmpty()
                    where !g.Any ()
                    orderby curr
                    select curr + 1).First();

sql, Linq to Sql, .

0
var nextAvailable = myInts.Prepend(0).TakeWhile((x,i) => x == i).Last() + 1;

7 , , .

, 0 . 0 , . TakeWhile , , , .
- , , 1.
TakeWhile , , , Except, TakeWhile , , Enumerable n.
A response using Except generates a series of answers that are not needed to just get the first one. Linq can do some optimization with First (), but it is still much slower and requires more memory than TakeWhile.

0
source

All Articles