I need help on this problem:
Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number 669260267 contains: one 0, two 2s, four 6es, one 7, and one 9. mostFrequentDigit(669260267) returns 6. If there is a tie, return the digit with the lower value. mostFrequentDigit(57135203) returns 3.
This is the code I have now, but it doesn't work:
public static int mostFrequentDigit(int num)
{
int largestCount = 0;
int currentCount = 0;
String num0 = Integer.toString(num)
String mostFrequent = num0.substring(0,1);
for (int x = 0; x < num0.length(); x++)
{
if (num0.substring(x,x+1).equals(mostFrequent))
{
currentCount++;
}
if (currentCount > largestCount)
{
largestCount = currentCount;
mostFrequent = num0.substring(x,x+1);
}
}
return mostFrequent;
}