0

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);
5
  • Use a Set if you don't want duplicated data Commented Oct 24, 2017 at 0:57
  • Use a Hashmap if you want to count frequency Commented Oct 24, 2017 at 0:58
  • 2
    Use the solution I gave you 2 days ago which is located here in the last section of code. Commented Oct 24, 2017 at 1:32
  • Hint: Bucket sort Commented Oct 24, 2017 at 1:45
  • Hashmap isn't a sorting algorithm Commented Oct 24, 2017 at 3:24

4 Answers 4

1

Make sure that i != 0. Compare charArray [i] to charArray [i-1]. If they're not equal, then print out the result.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a TreeMap<Character, Integer> to keep sorted character order and for counting letter frequencies.

Map<Character, Integer> freqs = new TreeMap<>();
for (char c : charArray.length) {
    freqs.put(c, 1+freqs.getOrDefault(c, 0));
} 

You also don't need to sort the input string. (Especially not using bubble sort). Iterating the characters and adding/updating to the map will create the order.

2 Comments

If OP cannot use HashMap, I doubt the instructor would allow TreeMap.
Yes - I answered before that was added. Still, only says builtin sorting method can't be used. Doesn't mention data structures
0

What you need to do is a Bucket Sort

In brief, create a int array of length 26, which represent 26 characters (Let's call this the "counter array"

Loop through your string. For each character encountered, increment the corresponding int in the counter array.

After looping through the input string, the counter array will contains the number of occurrence for each of 26 characters

The rest should be straight-forward I believe, which you just need to loop thru the counter array and print corresponding output

Spoiler ahead: sample code. Don't read if you haven't tried to write your code

String input = "The quick brown fox jumps over a lazy dog"; int[] counter = new int[26]; for (char c : input.toLowerCase().toCharArray()) { if (c >= 'a' && c <= 'z') { counter[c-'a']++; } } for (int i = 0; i < counter.length; ++i) { System.out.println("" + (char)('a' + i) + " occurs " + counter[i] + " times"); }

Comments

0

Solution: updated without hashmap and sorting:

Algo: 1.Create temp array of 26 alphabet and put frequency count ..i.e

0-> a, 
1-> b....

2.Iterate array to print the result

public static void countFrequency(){
String user = "Harshal";
user = user.toLowerCase();
    char ch[]= user.toCharArray();

    int arr[] = new int[26];

    for(char c : ch){
        arr[c-97]++;
    }

    for(int k=0;k<26;k++){
        if(arr[k]!=0){
            int val = k+97;
            System.out.println((char)val + " freq: " + arr[k]);
        }

    }

    }

1 Comment

Helpful, but I can't use the sort function or hashmaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.