Home > Blockchain >  Incorrect loop result
Incorrect loop result

Time:11-05

First array is filled randomly, its length is set from the console .I have an array in which I need to write a sub-array of repeating numbers and the number of repeating numbers. For example {3 3 3 3 3 4}, 3 3 3 3 is the repeating numbers, 4 is their number.

The problem is that if there are repeating numbers at the end of the array, the loop doesn't output them and if I enter the debug mode, I can see that not all numbers are written. What could be the problem?

for (int i = 1; i < array.Length; i  )
{
    if (array[i - 1] == array[i])
    {
        if((i   1) != array.Length)
        {
            duplicateCount  ;
            addArray = array[i - 1];
            duplicate  = addArray   " ";                        
        }
        else
        {
            duplicateCount  ;
            lastElement = array[i - 1];
            duplicate  = lastElement;
        }                 
    }
    else
    {                    
        if (duplicateCount != 1)
        {
            addPrevious = array[i - 1];
            duplicate  = addPrevious   " ";
            duplicateArrays.Add(duplicate   duplicateCount);
            duplicate = "";
            duplicateCount = 1;
        }
        else
        {
            duplicateCount = 1;
        }
    }
}

duplicateArrays.Add(duplicate   duplicateCount);      

CodePudding user response:

While the Antidisestablishmentarianism code is the shortest one, it uses '^' which is index from end operator introduced in C# 8.0. I'm not carping about that but it's a difference worth knowing. Compiling the same code in the older version will generate a compile time exception. I'll be giving a long yet beginner friendly code which is pretty self-explanatory if you read it twice.

static void Main(string[] args)
{
    int[] RandomArray = new int[] { 1, 2, 2, 3, 4, 4, 3, 1, 5, 2, 9, 8, 9, 8, 8, 5, 3, 4, 1 };
    int RandomArrayLength = RandomArray.Length;
    List<int[]> SubArrayList = new List<int[]>();
    List<int> visited = new List<int>();
    for (int i = 0; i < RandomArrayLength; i  )
    {
        int elem = RandomArray[i];
        if (!visited.Contains(elem))
        {
            visited.Add(elem);
            List<int> templist = new List<int>();
            for (int j = 0; j < RandomArrayLength; j  )
            {
                if (elem == RandomArray[j])
                {
                    templist.Add(elem);
                }
            }
            if (templist.Count > 1) //You can remove this condition if you want to include all the elements
            {
                int elemCount = templist.Count;
                List<int> sublist = new List<int>();
                sublist.AddRange(templist);
                sublist.Add(elemCount);
                SubArrayList.Add(sublist.ToArray());
            }
        }
        else
        {
            continue;
        }
    }
    int SubArrayListCount = SubArrayList.Count;
    for (int i = 0; i < SubArrayListCount; i  )
    {
        int[] SubArr = SubArrList[i];
        int SubArrCount = SubArr.Length;
        for (int j = 0; j < SubArrCount; j  )
        {
            Console.Write(SubArr[j].ToString()   " ");
        }
        Console.WriteLine();
    }
}

Output:

CodePudding user response:

Substituting an initialized array since it sounds like you have populating the array correct:

static void Main()
{
    int[] array = new int[] { 9, 9, 9, 8, 7, 6, 6, 6, 6 };
    List<int[]> subarrays = new();

    for (int x = 0; x < array.Length; x  )
    {
        int temp = x;

        while (x < array.Length - 1 && array[x] == array[x   1])
            x  ;

        if (x > temp)
        {
            int[] temparray = Enumerable.Repeat(array[temp], x - temp   2).ToArray();
            temparray[^1] = x - temp   1;
            subarrays.Add(temparray);
        }
    }

    foreach (int[] item in subarrays)
        Console.WriteLine(string.Join(" ", item));
}

Output:

9 9 9 3
6 6 6 6 4

  • Related