Answers for "count function in map"

C++
0

Map - counting with map

// counting how many letters int the string
String s = "alma mater";
Map<Character, Integer> countOfLetters = new HashMap<>();
for (char c: s.toCharArray()) {
    if (countOfLetters.containsKey(c)) {
        countOfLetters.put(c, countOfLetters.get(c) + 1);
    }
    else {
        countOfLetters.put(c, 1);
    }
}
System.out.println(countOfLetters); // { =1, a=3, r=1, t=1, e=1, l=1, m=2}
int numberOfM = countOfLetters.get('m'); // 2
Posted by: Guest on April-25-2021
0

what does map.count() return in c++

int n; cin>>n;
long val;
map<int,long> mp3;
long long res=0;
while(n--){
	cin>>val;
	if(mp3.count(val)) 	res+=mp3[val]; 
    //mp3.count(val) returns 1 if val is present in the map already or 
    //the number of times the key K is present in the map container.
    if(mp2.count(val)) 	mp3[val*r]+=mp2[val];
	mp2[val*r]++;
}
cout<<res<<endl;
Posted by: Guest on June-08-2020

Browse Popular Code Answers by Language