Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,094 questions

40,775 answers

573 users

How to print characters that have odd frequencies of occurrence in a string with Java

1 Answer

0 votes
public class MyClass {
    public static void print_odd_frequencies_char(String s) { 
        int[] letters = new int[256]; 
         
        for (int i = 0; i < s.length(); i++) {
             if (Character.isLetter(s.charAt(i))) {
                 char ch = s.charAt(i); 
                 letters[(int)ch]++;
             }
        }
          
        for (int i = 0; i < 256; i++) { 
            if (letters[i] != 0 && letters[i] % 2 != 0) {
                System.out.println(Character.toString((char) i) + " " + letters[i]);
            }
        } 
    } 
    public static void main(String args[]) {
        String s = "java programming pro oo"; 
         
        print_odd_frequencies_char(s);
    }
}
 
 
/*
run:
 
a 3
i 1
j 1
n 1
r 3
v 1
 
*/

 





answered Nov 27, 2019 by avibootz
edited Nov 27, 2019 by avibootz
...