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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to find unique characters in a string with Java

1 Answer

0 votes
import java.util.HashMap;
import java.util.Arrays;
import java.util.Map;

public class MyClass {
    static char[] get_unique_characters(String s) {
        char[] arr = s.toCharArray();

        Map<Character, Integer> map = new HashMap<>();
        
        for (char ch : arr) {
            if(map.containsKey(ch)) {
                int ch_counter = map.get(ch);
                map.put(ch, ++ch_counter);
            } else {
                map.put(ch, 1);
            }
        }
        Arrays.fill(arr, ' ');
        int i = 0;
        for (char ch : map.keySet()) {
            if (map.get(ch) == 1) {
                arr[i++] = ch;
            }
        }
        return arr;
    }
    public static void main(String args[]) {
        String s = "java php c python c#";
 
        Map<Character, Integer> map = new HashMap<>();
        
        char[] arr = get_unique_characters(s);

        for (char ch : arr) {
            if (!Character.isWhitespace(ch))
                System.out.println(ch);
        }
    }
}
  
 
 
  
/*
run:
    
 
#
t
v
y
j
n
o
 
*/

 



answered Jun 28, 2019 by avibootz
...