wiwiedbulu
wiwiedbulu

Reputation: 39

C# check missing range from specified Range

a = 1-30
b = 40-50
c = 60-100

The range above is 1-100. This range miss 31-39.

Does C# has a function that check whether specified range (1-100) missing any range (Ex:31-39) or missing value (Ex: 31)?

Regards

Upvotes: 0

Views: 263

Answers (2)

Marzouk
Marzouk

Reputation: 2714

you can create your own method like that:

    public List<int> CheckRange(int FirstRangeEnd, int SecondRangeStart)
    {
        List<int> missing = new List<int>();

        if (SecondRangeStart - FirstRangeEnd == 0)
        {
            missing.Add(-1);
            return missing;
        }
        for (int i= 0; i<= SecondRangeStart - FirstRangeEnd; i++)
        {
            missing.Add(FirstRangeEnd + i);
        }
        missing.RemoveAt(0);
        missing.RemoveAt(missing.Count - 1);
        return missing;
    }

Upvotes: 1

Maor Veitsman
Maor Veitsman

Reputation: 1564

Assuming 'input' contains the input, the following code will return true if any numbers are missing from the range:

bool result = Enumerable.Range(1, 100).Except(input).Any();

Upvotes: 3

Related Questions