Hi I am trying to go through a two-dimensional array (specifically a 4x4 array) and both find any numbers that repeat, then count the number of times that the number repeats. So far I have 4 for loops that work however do more than what i really want.
int counter1 =1;
String huh="";
for (int x = 0; x< dataTable.length; x++)
{
for (int y=0; y< dataTable.length; y++)
{
for (int z = 0; z< dataTable.length; z++)
{
for (int a=0; a< dataTable.length; a++)
{
if ( x != z && x !=a && y != z && y !=a)
{
if (dataTable[x][y] == dataTable[z][a])
{
counter1++;
}
}
}
}
if (counter1 > 1)
{
huh += ("\n " + dataTable[x][y] + " repeats " + counter1 + " times!");
}
counter1=1;
}
}
Basically this works in the sense that it compares every number in my array with every other number including itself (but the if statement keeps it from counting itself). Basically i need the output to state something simple like
The number 3 repeats 3 times
However with the way that my setup works it would add to the string that same statement each time it compared the number 3 in each of its places in my array. So is my method correct at all and only needs some tweaking? or is it wrong altogether and i need something totally different? I am only in a beginner programming class at my college so we only know the basics of java so far like arrays, loops, and a few other things.