Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find Highest Repetitive Count in a integer array.
       [1,2,5,4,4,6,7,3,3,5,2,4]

class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length - 1 ; j++)
            {

               if(array[j] == array[j+1])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}


What I have tried:

class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length - 1 ; j++)
            {

               if(array[j] == array[j+1])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}
Posted
Updated 31-Aug-20 21:05pm
v2
Comments
ZurdoDev 31-Aug-20 16:04pm    
And what is your question?
Arfat M 31-Aug-20 23:00pm    
Find highest repetitive number and how many times that occured in an current array
ZurdoDev 1-Sep-20 7:04am    
That is not a question. That is a statement. Notice, a question should have one of these silly symbols "?".
George Swan 1-Sep-20 4:08am    
There's an answer here that uses the same array values as in your example: https://stackoverflow.com/questions/20765589/finding-duplicate-integers-in-an-array-and-display-how-many-times-they-occurred

If you want to get longest repetition, take a look at my past answer: Longest repeating subsequence using only LINQ[^]

If you just want to get the highest count of item repeated in array, you can try this:
C#
int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };

var groupped = array
	.GroupBy(x=> x)
	.OrderByDescending(grp=> grp.Count())
	.FirstOrDefault();
	
Console.WriteLine($"{groupped.Key} is repeated {groupped.Count()} time(s).");


If you want to display the value and the number of times repeated, use this:
C#
var groupped = array
	.GroupBy(x=> x)
	.OrderByDescending(grp=> grp.Count())
	.ToList();
	
foreach(var d in groupped)
	Console.WriteLine($"{d.Key} is repeated {d.Count()} time(s).");
 
Share this answer
 
Comments
Sandeep Mewara 1-Sep-20 2:35am    
+5
Maciej Los 1-Sep-20 3:04am    
Thank you, Sandeep.
[no name] 1-Sep-20 13:08pm    
Yeah, a 5
Maciej Los 1-Sep-20 16:43pm    
Thank you, Bruno.
this:
C#
if(array[j] == array[j+1])
   count = count + 1;

is counting every 2 identical consecutive numbers
C#
int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
//                         *  *        *  *                  *   *

Changing to
C#
if(array[i] == array[j])
   count = count + 1;

will count every occurrences of number at position i

This code will count occurrences of number at position i
C#
class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            int count = 1;
            for (int j = i+1; j < array.Length - 1 ; j++)
            {

               if(array[i] == array[j])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}

Now, you have to keep track of highest count and its position.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900