Rearranging numbers to get the largest number
while (num != 0) {
    if (num == 0) {
        break;
    }



That if is redundant and can be removed.

Using a StringBuilder for largestNumber is roughly twice as fast on my machine.

Avoiding strings altogether is faster still:

int largestNumber = 0;
for (int i = 9; i &gt;= 0; i--) {
  for (int j = 0; j &lt; times[i]; j++) {
    largestNumber = largestNumber * 10 + i;
  }
}
return largestNumber;


You might want to return a long instead of an int so that, for example, largestNumber(Integer.MAX_VALUE) returns 8776444321, as expected.