Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find available numbers from a list of numbers in a particular range

Tags:

c#

Scenario: Database has table with list of account numbers. Account numbers range from 0-9999. Customer is allowed to make account numbers for customers within that range, as they see fit.

Need: I am producing a report that shows unused account numbers in range format. So, I need a list of strings, showing available account numbers, in range format.

Example: Account numbers 0, 1, 2, 4, 20, 21, 22 are all being used in data. So the result list would be...

3
5-19
23-9999

Been stumbling around on this all day. How to do this with straight-up c#?

like image 309
Chase Avatar asked Dec 19 '22 22:12

Chase


1 Answers

Use Range and Except

var acctNos = new List<int>() { 0,1,2,4,20,21,22 };

var unusedAcctNos = Enumerable.Range(0,9999).ToList().Except(acctNos);

Then to group contiguous integers, modify the accepted solution given here.

like image 122
Josh Avatar answered May 15 '23 23:05

Josh