简体   繁体   中英

Find first free element

I looking for query which return first number not available in list

int[] list = new int[] { 1,4,2,5,6,7 };

For above example I expect to have result 3.

Perhaps something like this:

int result = Enumerable.Range(1, list.Length)
                       .Where(i => !list.Contains(i))
                       .FirstOrDefault();

This will return 0 if list contains all integers from 1 to n .

var first = Enumerable.Range(1, list.Max()).Except(list).First();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM