0

I want to count last zero to next zero's length(Between last zero and next zero), for example:

int[] Array1 = {0,0,1,4,3,1,0,0,0};
int[] Array2 = {0,0,0,0,0,2,5,3,2,1,0,0,0,0,0,0,1,2,5,3,3,2,0,0,0,0};

Wish result:

Array1: 4 (from array1[2] -> array1[5])

Array2: 5 (from array1[5] -> array1[9]) & 6 (from array1[16] -> array1[21])

For array2, I want to get 5 & 6.

How can I determine size of an special array (length / number of items) in C# for below sample?

5
  • Welcome! Can you clarify what you mean by the request to "count last zero to next zero's length"? It would also help if you can use code formatting and the syntax for the appropriate language, e.g., dotnet var array1 = new int[] {0, 0, 1, 4, 3, 1, 0, 0, 0} Commented May 15, 2020 at 2:18
  • What did you mean by '''for array2 I want get result 12 & 10'''? There is no element in array2 with these values. Commented May 15, 2020 at 2:22
  • Sorry for my fault, correct is '''for array2 I want get result 5 & 6'''? I had modified. Commented May 15, 2020 at 2:46
  • 1
    Stack Overflow is not intended to be a code-writing service. Please fix your question so that it includes a good minimal reproducible example which shows clearly what you've already tried, as well as a clear and detailed explanation of what that code does, what you want it to do instead, and what specifically you are having trouble figuring out. Commented May 15, 2020 at 2:59
  • 1
    @FrankKuo - If you make a mistake in your question and someone has referred to the mistake in their answer, please edit the question to make it clear what your mistake was and what you actually intended. Don't make the answer confusing or look foolish with an edit. I've fixed your edit for you. Commented May 15, 2020 at 3:01

2 Answers 2

2

This handles the 5 and 6 for array 2 just fine:

public IEnumerable<int> CountNonzeroSequences(IEnumerable<int> input)
{
    int count = 0;
    foreach(var item in input)
    {
        if (item != 0) count++;
        else
        {
           if (count > 0) yield return count;
           count = 0;
        }
    }
    if (count > 0) yield return count;
}

You can use it like this:

int[] arr1 = {0,0,1,4,3,1,0,0,0};
int[] arr2 = {0,0,0,0,0,2,5,3,2,1,0,0,0,0,0,0,1,2,5,3,3,2,0,0,0,0};

foreach(var result in CountNonzeroSequences(arr1)) 
{
    Console.WriteLine(result);
}
Console.WriteLine();
foreach(var result in CountNonzeroSequences(arr2)) 
{
    Console.WriteLine(result);
}
Sign up to request clarification or add additional context in comments.

5 Comments

very nicely done. I wonder what the result should be for this array: int[] arr3 = {1,2,3,4,0,0,0,1,2,3,0,0,9,0};
Run it and see. Note I did just add line for the case where the array doesn't end with a 0.
I don't think the Edit helped. With the new line `if (count > 0) yield return count;' at the end I am getting the following results: "1 | 2 | 3 | 4 | 4 | 1 | 2 | 3 | 3 | 1" for this array "int[] a3 = {1,2,3,4,0,0,0,1,2,3,0,0,9};".
@Barns Odd, I get 4 | 3 | 1 dotnetfiddle.net/boYjnB
OK! That comment was based on using the EDIT that still had the line if (item == 0). With the newest EDIT it works. The only thing I am uncertain of is how the OP wanted the cases handled where the array does not have leading zeros. Both these arrays have the same result: int[] a3 = {0,1,2,3,4,0,0,0,1,2,3,0,0,9}; and int[] a4 = {1,2,3,4,0,0,0,1,2,3,0,0,9}; :: 4 | 3 | 1
1

Certain not quite as compact, but the following also handles the situation when the lead items in the array are not zero. And with the addition of the "target" parameter count can be found between any integer value.

    private static IEnumerable<int> CountSequenceInArray(IEnumerable<int> seq, int target)
    {
        var foundLastZero = false;
        int startCount = 0;
        foreach(var item in seq)
        {
            var isZero = item.Equals(target);
            if(!isZero && foundLastZero)
            {
                startCount++;
                continue;
            }
            else if(isZero && startCount > 0)
            {
                yield return startCount;
                startCount = 0;
            }
            foundLastZero = isZero;
        }
    }

With the following test set:

    static void Main(string[] args)
    {
        Console.WriteLine(“—— Start ——”);
        int[] a0 = {0,0,0,0,1};
        int[] a1 = {0,0,1,2,3,4,0,0,0};
        int[] a2 = {0,1,2,3,4,0,0,0,1,2,3,0,0,9,0};
        int[] a3 = {1,2,3,4,0,0,0,1,2,3,0,0,9};
        int[] a4 = {1,2,3,4,0,1,2,0,1,2,3,0,1,0,1,2,3,0,0,9,0,8};
        var listArrays = new List<int[]>();
        listArrays.Add(a0);
        listArrays.Add(a1);
        listArrays.Add(a2);
        listArrays.Add(a3);
        listArrays.Add(a4);

        foreach(var arr in listArrays)
        {
            Console.WriteLine();
            CountSequenceInArray(arr, 0).ToList().ForEach(x => Console.Write($"Zeros {x} | "));
        }
    }

Results:

(a0 yields no results)
Zeros 4 | 
Zeros 4 | Zeros 3 | Zeros 1 | 
Zeros 3 | 
Zeros 2 | Zeros 3 | Zeros 1 | Zeros 3 | Zeros 1 |

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.