So I have a program in java that takes a string from user input, sorts it, finds the frequencies, and prints it in alphabetical order. My only issue is that it also prints duplicates. So, if the letter d appeared three times in a string, it would print like this:
d freq: 3
d freq: 3
d freq: 3
For this project, I am not allowed to use any built-in java sorting functions (hashmaps included). Any suggestions on how to stop this? Here's my code. Thank you!
char[] charArray = userSort.toCharArray();
char tempChar;
for (int i = 0; i < charArray.length; i++) {
for (int j = 0; j < charArray.length; j++) {
if (charArray[i] < charArray[j]) {
tempChar = charArray[i];
charArray[i] = charArray[j];
charArray[j] = tempChar;
}
}
}
String sortedString = "";
for (int i = 0; i < charArray.length; i++) {
userSort += charArray[i];
}
System.out.println(sortedString + "\n");
int counter;
sortedString = "";
for (int i = 0; i < charArray.length; i++) {
counter = 0;
for (int j = 0; j < charArray.length; j++) {
if (charArray[i] == charArray[j]) {
counter++;
}
}
if (!sortedString.contains("Char: " + charArray[i])) {
if (sortedString.equals("")) {
sortedString += " " + charArray[i] + " freq: " + counter + "\n";
} else {
sortedString += " " + charArray[i] + " freq: " + counter + "\n";
}
}
}
System.out.println(sortedString);