Please help me with the code to find the most repeated number in an array. The code below is not working perfectly.

``````int p =o;
int[,] numbers = {{1, 2, 0, 6 },
{5, 6, 7, 0 },
{9, 3, 6, 2 },
{6, 4, 8, 1}};
int []A = new int [16];
foreach (int n in numbers)
{
A[P] = n;
P++;
}

P = -1;

for (int x = 0; x < A.Length; x++)
{
for (int y = 0; y < A.Length; y++)
{
if (A[x] == A[y])
{
if (x != y)
{
if (A[x] > P) P = A[x];
}
}
}
}

if (P == -1) Console.WriteLine("There's No repeated number ");
else Console.WriteLine("The most repeated number is {0}", P);
``````

Edited by Dani: Formatting fixed

5
Contributors
7
Replies
12
Views
7 Years
Discussion Span
Last Post by Bridgekeepers

Please use code tags next time. :)

``````int P = 0;
int[,] numbers = {{1, 2, 0, 6 },
{5, 6, 7, 0 },
{9, 3, 6, 2 },
{6, 4, 8, 1}};
int[] A = new int[16];

foreach (int n in numbers)
{
A[P] = n;
P++;
}

P = -1;

for (int x = 0; x < A.Length; x++)
{
for (int y = 0; y < A.Length; y++)
{
if (A[x] == A[y])
{
if (x != y)
{
if (A[x] > P) P = A[x];
}
}
}
}

if (P == -1) Console.WriteLine("There's No repeated number ");
else Console.WriteLine("The most repeated number is {0}", P);``````

Could you comment your code? It is a little hard to understand and it might help you solve the problem.

In short I need code for finding the most repeated number in any type of an array.
In the one I posted there's a 2D array. I turned it into a single dimensional array and then started looking for the numbers appearing more than once and print out the greatest amongst those numbers.

Edited by FELIGO: n/a

I am sorry but I'm not seeing how that code is supposed to work. It doesn't appear that you're storing the times a number is repeated anywhere.

Try this. It's not the best answer but certainly simpler than what you have above.

• Create Key/Value dictionary
• Walk through your array, add a key for each number and increment the value each time that element is repeated.
• Walk through the dictionary keys, and return the key with the highest value.

If that doesn't make sense I'll be glad to help.

try something like this:

``````static void Main(string[] args)
{
int[,] numbers = {{1, 2, 0, 6 },
{5, 6, 7, 0 },
{9, 3, 6, 2 },
{6, 4, 8, 1}};

int count = 0;
List<int> checkedNumbers = new List<int>();

foreach (int t in numbers)
{

if (!checkedNumbers.Contains(t))
{
foreach (int m in numbers)
{
if (m == t)
{
count++;
}
}
Console.WriteLine("Number {0} is Repeated {1} Times ", t, count);
count = 0;

}
}

}``````

this will work for any dimensions of array..

check the attached screen shot

Attachments

I bet you can do this with linq in one line!

I think that you have an error when counting the numbers in the line

``````foreach (int n in numbers)
{
A[P] = n;
P++;
}``````

Instead try to sonething like

``````int[] countOfNumbers = new int(10);
foreach (int m in numberArrayToCheck)``````

Sorry - a slip of the finger :-)

``````int[] countOfNumbers = new int(10);
foreach (int m in numberArrayToCheck)
countOfNumbers[m]++;``````

if all the values in the countOfNumbers array is 0 or 1, you don't have any repeating numbers.

And yes, Linq is certainly the more elegant way of doing this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.