Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

11,402 questions

15,552 answers

573 users

How to find duplicate 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_duplicate_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_duplicate_characters(s);

        for (char ch : arr) {
            if (!Character.isWhitespace(ch))
                System.out.println(ch);
        }
    }
}
  
 
 
  
/*
run:
    
 
p
a
c
h
 
*/

 




answered Jun 28 by avibootz
...