I want to print the most repeated value in an array. If two values get repeated for maximum number of times, then print the largest one.I don't know how to print the largest one.I tried this.It just prints the print the most repeated value in an array.
int[] a= { 3,2,3,2,2};
int count = 1, tempCount;
int repeated = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount > count)
{
repeated = temp;
count = tempCount;
}
}
System.out.println(repeated);
If suppose the array elements are "3,2,3,3,2,4,5,4,6,4" then it has to print 4.(no. 3 three times and no. 4 three times.....But 4 is the greatest no. so the output is 4). Now how can i change my code?