I have a file with 1000 characters. I readed that file and stored it into a string. Now : I have a String str with 1000 characters I have a char[] letters with length str.chars().distinct().count() - the number of unique chars in my string
I need to store each unique char from my String str to my char array - letters I also need to store the frequency of each char from String str somewhere It could be another array.
The index of each item in the letters array has to be the same as the index of the frequency of that element in the frequency array.
Afterwards I need to store all the data from both arrays in priority queue. The data has to be sorted with the lowest value at the top node.
For the first part do you suggest me using arrays at all ? If yes, how to approach with that counting and then how to sort both arrays regarding the frequency array where I stored the frequency ?
Time complexity is also important.
I wouldn't do that with arrays. Java has a nice library for collections that you can use instead of arrays. This code is not efficent but it shows the idea.
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
//iterate over your chars
for(char c : myString.toCharArray())
{
Character character = new Character(c);
if(map.containsKey(character))
{
map.put(character, new Integer(map.get(character).intValue()+1));
}else
{
map.put(character, new Integer(1));
}
}