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();
}
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();
}
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());
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us