Efficient way to find Frequency of a character in a String in java : O(n)

I'd use an array rather than a hash map. If we're limited to ascii, that's just 256 entries; if we're using Unicode, 64k. Either way not an impossible size. Besides that, I don't see how you could improve on your approach. I'm trying to think of some clever trick to make it more efficient but I can't come up with any.

Seems to me the answer is almost always going to be a whole list of characters: all of those that are used zero times.


This is probably clost to the most efficient it could be in Java. For convenience, I'm assuming we're using plain Ascii.

public List<Character> rarest(String s)
  int[] freq=new int[256];

  for (int p=s.length()-1;p>=0;--p)
    char c=s.charAt(p);
    if (c>255)
      throw new UnexpectedDataException("Wasn't expecting that");
  int min=Integer.MAX_VALUE;
  for (int x=freq.length-1;x>=0;--x)
    // I'm assuming we don't want chars with frequency of zero
    if (freq[x]>0 && min>freq[x])
  List<Character> rares=new ArrayList<Character>();
  for (int x=freq.length-1;x>=0;--x)
    if (freq[x]==min)
  return rares;

Any effort to keep the list sorted by frequency as you go is going to be way more inefficient, because it will have to re-sort every time you examine one character.

Any attempt to sort the list of frequencies at all is going to be more inefficient, as sorting the whole list is clearly going to be slower than just picking the smallest value.

Sorting the string and then counting is going to be slower because the sort will be more expensive than the count.

Technically, it would be faster to create a simple array at the end rather than an ArrayList, but the ArrayList makes slightly more readable code.

There may be a way to do it faster, but I suspect this is close to the optimum solution. I'd certainly be interested to see if someone has a better idea.