So I am building a huffman tree and I am in need of taking a String as input, and then create 2 arrays containing each letter and the number of occurrences of that letter in the original string, like this:
String s = "mississippi"
Should result in:
char[] charArr = {'m','i', 's', 'p'};
int[] count = {1,4,4,2};
There are alot of question regarding this and alot of examples on how to solve this, especially here on stackoverflow but the only one that I managed to get working was this:
private void findOccurences(String s) {
List<Character> original = new ArrayList<Character>(s.length());
List<Character> duplicateRemoved;
for (int i = 0; i < s.length(); i++) {
original.add(s.charAt(i));
}
duplicateRemoved = new ArrayList<Character>(original);
// Remove duplicates from second list.
Set<Character> hs = new HashSet<Character>();
hs.addAll(duplicateRemoved);
duplicateRemoved.clear();
duplicateRemoved.addAll(hs);
charFreqs = new int[duplicateRemoved.size()];
charArr = new char[duplicateRemoved.size()];
for (int i = 0; i < charArr.length; i++) {
char c = duplicateRemoved.get(i);
int count = Collections.frequency(original, c);
charArr[i] = c;
charFreqs[i] = count;
}
}
But it feels very cumberstone and it also scrambles the order of the letters in the array. If I use this my resulting array is as follows:
char[] charArr = {'p','s', 'i', 'm'};
Are there any better way to do what I want?