title: Efficiently sorting a String by the number of occurrences of each of its characters
body: <p>I'm trying to sort a string by the number of occurrences of each of its characters, with the most frequent at the beginning and rarest at the end. After sorting, I would need to remove all the character repeats. Because examples are always clearer, the program should do the following:</p>
  
  <pre><code>String str = "aebbaaahhhhhhaabbbccdfffeegh";
  String output = sortByCharacterOccurrencesAndTrim(str);
  </code></pre>
  
  <p>In this case, the 'sortByCharacterOccurrencesAndTrim' method should return:</p>
  
  <pre><code>String output = "habefcdg"
  </code></pre>
  
  <p>In the case where 2 characters have the same occurrence, their order in the returned string doesn't matter. So "habefcdg" can also equal "habfecgd", because both 'f' and 'e' occur 3 times and both 'd' and 'g' occur once.</p>
  
  <pre><code>"habefcdg" would effectively be the same as "habfecgd"
  </code></pre>
  
  <p><strong><em>Note:</em></strong> I would like to point out that <em>performance matters</em> in this case, so I would prefer the most efficient method possible. I say this because the string length can range from 1 to the max length (which I think is the same as Integer.MAX_VALUE, but not sure), so I want to minimize any potential bottlenecks.</p>