Answers for "number of disnict characters of a string"

C++
0

finding no of unique characters in a string c++

int countDistinct(string s) 
{ 

    unordered_map<char, int> m; 

    for (int i = 0; i < s.length(); i++) { 
        m[s[i]]++; 
    } 

    return m.size(); 
}
Posted by: Guest on July-15-2020
0

find unique characters in java

//  I would use linkedHashMap since it doesn't accept
//  duplicate values

String str = "abbcdddefffggghjilll";
        String [] arr = str.split("");
        Map<String, Integer> mapStr = new LinkedHashMap<>();

        for (int i=0 ; i < arr.length ; i++){
            if (!mapStr.containsKey(arr[i])){
                mapStr.put(arr[i],1);
            } else{
                mapStr.put(arr[i],mapStr.get(arr[i])+1);
            }
        }

        for (Map.Entry<String,Integer> map : mapStr.entrySet()) {
            if(map.getValue()==1) {
                System.out.print(map.getKey());
            }
        }
Posted by: Guest on January-14-2021

Code answers related to "number of disnict characters of a string"

Browse Popular Code Answers by Language