I want to get rid of unneccessary repetiton of cycles I have a code that should read diameters of barrels from a txt file. If a barrel is smaller than the last one, smaller gets put inside bigger one. One package like that is lied on a stack. at the end, code should give out how many stacks we get and how many barrels were in a file. also sum of diameters.
If one number is repeated 3 times, we get 3 stack, if we have 5 repeating nmbers, then 5.
What can I do that I wouldn´t have to write simillar blocks n times.
using System;
using System.Collections.Generic;
using System.IO;

namespace TaaviSimsonTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextReader reader = File.OpenText(&quot;C:\\temp\\andmed2&quot;))
            {
                int sum = 0;
                string line = string.Empty;
                List&lt;int&gt; numbersList = new List&lt;int&gt;();
                while ((line = reader.ReadLine()) != null)   
                {
                    int i = int.Parse(line);    
                    sum += i;
                    numbersList.Add(int.Parse(line));
                }
                int[] numbersListArray = numbersList.ToArray();
                int numberOfStacks = numbersListArray.Length;
                //Max number of stacks equals to inital array length

                Array.Sort(numbersListArray);       //Array is ascending orded
                Array.Reverse(numbersListArray);    //Array in descending order
                
                //Puts smaller barrels inside bigger. Decreases number of stacks.
                List&lt;int&gt; repeatedBrarrelSizes = new List&lt;int&gt;();
                for (int j = 0; j &lt; (numbersListArray.Length - 1); j++)
                {
                    if (numbersListArray[j] &gt; numbersListArray[j + 1])
                    {
                        numberOfStacks--;
                    } 
                    else if (numbersListArray[j] == numbersListArray[j + 1])
                    {
                        repeatedBrarrelSizes.Add(numbersListArray[j + 1]);
                    }
                }
                int[] repeatedBarrelSizesArray = repeatedBrarrelSizes.ToArray();

                //Repeats the cycle with repeating numbers
                List&lt;int&gt; repeatedBarrelSizes2 = new List&lt;int&gt;();
                for (int k = 0; k &lt; (repeatedBarrelSizesArray.Length-1); k++)
                {
                    if (repeatedBarrelSizesArray[k] &gt; repeatedBarrelSizesArray[k+1])
                    {
                        numberOfStacks--;
                    }
                    else if (repeatedBarrelSizesArray[k] == repeatedBarrelSizesArray[k + 1])
                    {
                        repeatedBarrelSizes2.Add(repeatedBarrelSizesArray[k + 1]);
                    }
                }
                int[] repeatedBarrelSizes2Array = repeatedBarrelSizes2.ToArray();

                //Repeats the cycle again, until no barrels left
                List&lt;int&gt; repeatedBarrelSizes3 = new List&lt;int&gt;();
                for (int k = 0; k &lt; (repeatedBarrelSizes2Array.Length - 1); k++)
                {
                    if (repeatedBarrelSizes2Array[k] &gt; repeatedBarrelSizes2Array[k + 1])
                    {
                        numberOfStacks--;
                    }
                    else if (repeatedBarrelSizes2Array[k] == repeatedBarrelSizes2Array[k + 1])
                    {
                        repeatedBarrelSizes3.Add(repeatedBarrelSizes2Array[k + 1]);
                    }
                }
                int[] repeatedBarrelSizes3Array = repeatedBarrelSizes3.ToArray();

                foreach (int value in repeatedBarrelSizes3Array)
                {
                    Console.WriteLine(value + &quot; &quot;);
                }

                Console.WriteLine(&quot;Sum: &quot; + sum);
                Console.WriteLine(&quot;Number of barrels: &quot; + numbersListArray.Length);
                Console.WriteLine(&quot;Stacks: &quot; + numberOfStacks);
            }
        }
    }
}
DRY = Don't Repeat Yourself.
If you have repeating parts of code, something went wrong then. Also you may use Linq.
Consider this example.
static void Main(string[] args)
{
    string[] lines = File.ReadAllLines(@&quot;C:\temp\andmed2&quot;);
    List&lt;int&gt; numbersList = lines.Select(line =&gt; int.Parse(line)).OrderByDescending(x =&gt; x).ToList();
    int sum = numbersList.Aggregate(0, (x, y) =&gt; x + y);
    int numberOfStacks = numbersList.Count;

    List&lt;int&gt; repeatedBarrelSizes = numbersList;
    int count;
    do
    {
        count = 0;
        List&lt;int&gt; result = new List&lt;int&gt;();
        for (int i = 0; i &lt; repeatedBarrelSizes.Count - 1; i++)
        {
            if (repeatedBarrelSizes[i] &gt; repeatedBarrelSizes[i + 1])
                count++;
            else if (repeatedBarrelSizes[i] == repeatedBarrelSizes[i + 1])
                result.Add(repeatedBarrelSizes[i + 1]);
        }
        repeatedBarrelSizes = result;
        numberOfStacks -= count;
    } while (count &gt; 0);

    Console.WriteLine(string.Join(&quot; &quot;, repeatedBarrelSizes));

    Console.WriteLine(&quot;Sum: &quot; + sum);
    Console.WriteLine(&quot;Number of barrels: &quot; + numbersList.Count);
    Console.WriteLine(&quot;Stacks: &quot; + numberOfStacks);
}

I'm sorry but I can't test it because I have no source file with numbers to compare the results.
Total: two loops.