Saturday, 17 January 2015

Find duplicate charcters with occurrences in a string

 A string contain some duplicate characters.find duplicate charcter with occurrences
public class DuplicateCharsInString {
     public void findDuplicateChars(String str){   
        Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
        char[] chrs = str.toCharArray();
        for(Character ch:chrs){
            if(dupMap.containsKey(ch)){
                dupMap.put(ch, dupMap.get(ch)+1);
            } else {
                dupMap.put(ch, 1);
            }
        }
        Set<Character> keys = dupMap.keySet();
        for(Character ch:keys){
            if(dupMap.get(ch) > 1){
                System.out.println(ch+"--->"+dupMap.get(ch));
            }
        }
    }
    
    public static void main(String a[]){
        DuplicateCharsInString dcs = new DuplicateCharsInString();
        dcs.findDuplicateChars("javatipsall");
    }
}
=============================
Output:
a--->3
l--->2




 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).



No comments:

Post a Comment