Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 2, 2025 11:44
Show Gist options
  • Save Strelok78/c3c14c2f4f604834b4a149a16bb3b434 to your computer and use it in GitHub Desktop.
Save Strelok78/c3c14c2f4f604834b4a149a16bb3b434 to your computer and use it in GitHub Desktop.
get number frequently repeated
using System;
namespace iJuniorPractice
{
class Program
{
static void Main(string[] args)
{
int[] numbers = {7, 7, 7, 42, 42, 9, 19, 19, 19, 31,
31, 64, 64, 64, 12, 12, 26, 53, 53, 53,
71, 71, 4, 4, 4, 4, 87, 87, 22, 22};
int repeatedNumberCount = 1;
int repeatedNumber = numbers[0];
int maxRepeatedNumber = repeatedNumber;
int maxRepeatedCount = 0;
for (int i = 1; i < numbers.Length; i++)
{
if (repeatedNumber == numbers[i])
{
repeatedNumberCount++;
}
else
{
if (repeatedNumberCount > maxRepeatedCount)
{
maxRepeatedNumber = repeatedNumber;
maxRepeatedCount = repeatedNumberCount;
}
repeatedNumber = numbers[i];
repeatedNumberCount = 1;
}
}
if (repeatedNumberCount > maxRepeatedCount)
{
maxRepeatedNumber = repeatedNumber;
maxRepeatedCount = repeatedNumberCount;
}
Console.WriteLine($"The most repeated number is {maxRepeatedNumber} and it was repeated {maxRepeatedCount} times.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment