Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 12:07
Show Gist options
  • Save Strelok78/cca6f6796ac50e0a8e24138c76a2d781 to your computer and use it in GitHub Desktop.
Save Strelok78/cca6f6796ac50e0a8e24138c76a2d781 to your computer and use it in GitHub Desktop.
Outputs to the console the array itself, the number that is repeated the most number of times in a row and the number of its repetitions. (An additional array is not created on purpose.)
namespace MyCode
{
internal class Program
{
static void Main(string[] args)
{
int arraySize = 30;
int[] numbers = new int[arraySize];
int maxValue = 10;
int counter = 0;
int length = 0;
int value = 0;
int reset = 0;
int temporary = numbers[0];
Random random = new Random();
for (int i = 0; i < arraySize; i++)
{
numbers[i] = random.Next(maxValue);
Console.Write(numbers[i] + " ");
if (numbers[i] == temporary)
{
counter++;
if (counter > length)
{
value = temporary;
length = counter;
}
}
else
{
temporary = numbers[i];
counter = reset;
counter++;
}
}
Console.WriteLine($"\n\nValue: {value}, was repeated {length} times");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment