Skip to main content

Command Palette

Search for a command to run...

Finding the frequency of each characters

Published
1 min read
A

BTech CSE student at IIIT Ranchi | Aspiring Software Engineer | Passionate about coding, building innovative projects, and solving real-world problems through technology. Currently diving into web development, Machine learning and deep learning. Always learning, creating, and pushing boundaries! #Tech #Coding

#Finding frequency of each character in an string without using the hash-map…

// printing the frequency of each character..without using hashmap..
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    vector<int> v(26,0);//frequency array..special array..
    for(int i=0;i<s.length();i++){
        char ch = s[i];
        int x = (int)ch;
        int idx = x-97;
        v[idx]+=1;
    }
    for(int i=0;i<v.size();i++){
        if(v[i]!=0){
            int ascii = 97+i;
            cout<<(char)ascii<<" "<<v[i]<<endl;
        }
    }
    return 0;
}

You can also do it easily using a char and integer map .. bye bye ..

More from this blog

Untitled Publication

28 posts

Currently pursuing Bachelor of technology (Computer science and Engineering) from INDIAN INSTITUTE OF INFORMATION TECHNOLOGY RANCHI,JHARKHAND..