using System;
namespace 2258864
{
class Program
{
static void MostRepeatedInARow(int[] array)
{
int MostRepCount = 0, count = 1;
int? Mostrepeated = null;
for (int i = 1; i < array.Length; i++)
{
if (array[i] == array[i - 1])
{
count++;
}
else
{
if (count > MostRepCount)
{
MostRepCount = count;
Mostrepeated = array[i - 1];
count = 1;
}
else
{
count = 1;
}
}
}
Console.WriteLine($"Number {Mostrepeated} repeated {MostRepCount} times in a row.");
}
static void Main()
{
int[] ListofNumbers = { 4, 2, 6, 6, 6, 6, 5, 5, 5, 4, 3, 5, 3, 3, 3, 3, 2 };
MostRepeatedInARow(ListofNumbers);
}
}
}
Asked
Active
Viewed 71 times
-1

Robert Harvey
- 178,213
- 47
- 333
- 501

dean ambrose
- 1
- 1
-
1Store the number and count of each unique number in a data structure of some sort (a `List
>`, perhaps?). Then you can do whatever you want with the resulting "histogram." – Robert Harvey Sep 17 '21 at 13:24 -
3I like Robert's idea, but I'd go with `Dictionary
` because it's easier to look up values with that, and because there's no need to support duplicate keys. – jason44107 Sep 17 '21 at 13:33 -
It might be worth taking a look at https://stackoverflow.com/questions/14879197/linq-query-data-aggregation-group-adjacent. (It is likely overkill, but does handle grouping of adjacent values.) – Andrew McClement Sep 17 '21 at 14:07
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 24 '21 at 13:49
2 Answers
0
A basic implementation:
using System;
using System.Collections.Generic;
static (IReadOnlyCollection<T> MostRepeatedItems, int LongestRepetition) FindLongestRepetitions<T>(IEnumerable<T> collection)
where T : IEquatable<T>
{
var currentLongestRepetitions = new HashSet<T>();
var longestRepetition = 0;
T previousItem = default;
var foundAny = false;
var currentRepetition = 0;
foreach (var item in collection)
{
if (!foundAny)
{
foundAny = true;
currentRepetition = 1;
}
else if (item.Equals(previousItem))
{
currentRepetition += 1;
}
else
{
currentRepetition = 1;
}
if (currentRepetition > longestRepetition)
{
currentLongestRepetitions = new HashSet<T> {item};
longestRepetition = currentRepetition;
}
else if (currentRepetition == longestRepetition)
{
currentLongestRepetitions.Add(item);
}
previousItem = item;
}
return (currentLongestRepetitions, longestRepetition);
}
int[] numbers = { 4, 2, 6, 6, 6, 6, 5, 5, 5, 4, 3, 5, 3, 3, 3, 3, 2 };
var (mostRepeatedNumbers, longestRepetition) = FindLongestRepetitions(numbers);
Console.WriteLine($"The most repeated numbers were: {string.Join(',', mostRepeatedNumbers)}.");
Console.WriteLine($"They were repeated {longestRepetition} times.");
Try it out here

Andrew McClement
- 1,171
- 5
- 14
-
1This code is acceptable and really does what I need it to do. But: Unfortunately my sensei will not get a similar answer. Because I have not learned so much yet.I need to find an easier way. Thank you so much for the support though – dean ambrose Sep 17 '21 at 15:16
0
try this
static void MostRepeatedInARow(int[] array)
{
List<(int, int)> repeated=new List<(int,int)>();
var count =1;
for (int i = 1; i < array.Length; i++)
{
if (array[i] == array[i - 1])
{
count++;
}
else
{
(int, int) n= (array[i-1],count);
repeated.Add(n);
count = 1;
};
}
repeated.Sort((s1, s2) => s2.Item2.CompareTo(s1.Item2));
var howManyResults=2; // repeated.Count for all
foreach (var item in repeated)
{
Console.WriteLine($"Number {item.Item1} repeated {item.Item2} times in a row.");
howManyResults--;
if(howManyreSults==0) break;
}
output
Number 6 repeated 4 times in a row.
Number 3 repeated 4 times in a row.
or
foreach (var item in repeated)
{
if (item.Item2 >1)
Console.WriteLine($"Number {item.Item1} repeated {item.Item2} times in a
row.");
else
{
Console.WriteLine("All another numbers don't repeat in a row.");
break;
}
}
output
Number 6 repeated 4 times in a row.
Number 3 repeated 4 times in a row.
Number 5 repeated 3 times in a row.
All another numbers don't repeat in a row.

Serge
- 40,935
- 4
- 18
- 45