-3

I'm trying to implement a method for checking consecutive numbers in C#. Given an integer list of n elements, it should return true/false if the numbers are consecutive. So for example, 12345, 45678, 54321 would all be true. And 435276, 243516, 974264 would be false.

My code seems to be performing as expected. But it's missing the end element.

for (int i = 0; i < inputList.Count - 1; i++)
{
    if (inputList[i] < inputList[i + 1])
    {
        Console.WriteLine($"{inputList[i]} is consecutive when compared to {inputList[i + 1]}");
        consecutiveCheck = true;
    }
    else
    {
        Console.WriteLine($"{inputList[i]} is not consecutive when compared to {inputList[i + 1]}");
        consecutiveCheck = false;
        break;
    }
}
5
  • 5
    These are not consecutive numbers, but increasing numbers. Jul 21, 2019 at 12:37
  • 1
    What do you mean missing the end element? For which input does it fail and how? Jul 21, 2019 at 12:44
  • Duplicate ? stackoverflow.com/questions/1940214/…
    – Calimero
    Jul 21, 2019 at 12:46
  • 1
    dotnetfiddle.net/wEPqLS What specifically is the issue with your code @Keva161? It seems to work fine. But it's missing the end element. Sure, the code is only giving two lines of output but that is expected. If you have three values then you have two comparisons.
    – mjwills
    Jul 21, 2019 at 13:42
  • 1
    Your code isn't guaranteed to output all elements to the console when consecutiveCheck is set to false, because the loop terminates due to the break statement.
    – samgak
    Jul 21, 2019 at 20:55

4 Answers 4

1

Here's a simple way to do it:

int[] inputList = new [] { 12345, 45678, 54321 };

bool all_increasing = inputList.Zip(inputList.Skip(1), (x0, x1) => x1 > x0).All(x => x);
0
0

What are Consecutive numbers

It is series of number which have equal distance.

Example of Consecutive numbers 1,2,3,4,5 1,3,5,7,9

Example of NO Consecutive numbers 1,2,4,8,11 1,3,6,10,11

            
            int diff = 0;
            
            for (var i = 0; i < numbersInput.Length-1; i++)
            {
                if(i==0)
                {
                    diff = numbersInput[i + 1] - numbersInput[i];
                }
                else if(numbersInput[i] + diff != numbersInput[i+1])
                {
                    return false;
                }

            }

            return true;
-2

You mean you are checking if they are increasing:

bool inc = true;
for (int i = 1; i < inputList.Count; i++)
{
    if (inputList[i] < inputList[i - 1])
    {
       inc=false;
       break;
    }
}

Console.WriteLine($"List has consecutive numbers: {(inc?"yes":"no")}");
4
  • 1
    My point is, this does not solve the problem. (I actually believe there is no problem) (PS. I did not downvote though) Jul 21, 2019 at 12:52
  • 1
    Read the question again, there is not. Jul 21, 2019 at 12:54
  • @JessedeWit, "Given an integer list of n elements, it should return true/false"... Jul 21, 2019 at 12:57
  • 1
    Yes, your code will work, but so will the TO's code. Jul 21, 2019 at 12:59
-3

This code will work :

    private static bool isConsecutive(int[] list)
    {
        switch (list.Length)
        {
            case 0:
                throw new ArgumentException("Value cannot be an empty collection.", nameof(list));
            case 1:
                throw new ArgumentException("This collection contains only one element.", nameof(list));
        }

        int direction = list[1]-list[0];

        for (var index = 0; index < list.Length; index++)
        {
            int nextIndex = index + 1;

            if (nextIndex >= list.Length)
            {
                continue;
            }

            int diff = list[nextIndex] - list[index];


            if (diff != direction)
            {
                return false;
            }

        }

        return true;
    }
5
  • 2
    This will return true for [1,2,1,2,1,2,1,2,1,2,1,2], and false for [1,3,5,7]. Jul 21, 2019 at 12:57
  • Now I think it's fine
    – Shehab
    Jul 21, 2019 at 13:03
  • 1
    This will work for [1,3,5,7], but will fail for [1,3,5,8], since the diff is not equal to the direction (3 != 2). Jul 21, 2019 at 16:51
  • Yes sure because this is what consecutive numbers is simple.wikipedia.org/wiki/Consecutive_integer
    – Shehab
    Jul 22, 2019 at 7:48
  • Quoting from the question: 12345, 45678, 54321 would all be true. And 435276, 243516, 974264 would be false Jul 22, 2019 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.