Common characters

How many times does the most common character in this file appear?

Click here to open the file.


The answer is 26.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Use a dictionary to keep track of character counts. Use the character as the dictionary key, and the character count incidence as the dictionary value.

Iterate through the file, reading the characters as you go. For each character, increment its count in the dictionary. When you reach the end of the file, find the maximum value stored in the dictionary.

* Python solution: *

# open the file and initialize the dictionary
f = open('cs_commonchar.txt', 'r') 
chars = {}

# for each character in the file, see if it is already in the dictionary
# if it is in the dictionary, increment its count
# if it is not in the dictionary, add it to the dictionary and set its count to 1
for c in f.read():
    if c in chars:
        chars[c] += 1
    else:
        chars[c] = 1

f.close() # close the file, we are done with it

# print the maximum value in the dictionary
print max(chars.values())

http://en.wikipedia.org/wiki/Letter_frequency 'e' is the most common character in English Language...

Shabbir Haider - 6 years, 5 months ago
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<bits/stdc++.h>
using namespace std;

int main(){
    string str;
    long long R[99],max=0;

    memset(R,0,sizeof R);
    cin>>str;

    for(int coy=0;coy<str.length();coy++){       
        switch(str[coy]){
            case 'a':
                R[0]++;
            break;
            case 'b':
                R[1]++;
            break;
            case 'c':
                R[2]++;
            break;
            case 'd':
                R[3]++;
            break;
            case 'e':
                R[4]++;
            break;
            case 'f':
                R[5]++;
            break;
            case 'g':
                R[6]++;
            break;
            case 'h':
                R[7]++;
            break;
            case 'i':
                R[8]++;
            break;
            case 'j':
                R[9]++;
            break;
            case 'k':
                R[10]++;
            break;
            case 'l':
                R[11]++;
            break;
            case 'm':
                R[12]++;
            break;
            case 'n':
                R[13]++;
            break;
            case 'o':
                R[14]++;
            break;
            case 'p':
                R[15]++;
            break;
            case 'q':
                R[16]++;
            break;
            case 'r':
                R[17]++;
            break;
            case 's':
                R[18]++;
            break;
            case 't':
                R[19]++;
            break;
            case 'u':
                R[20]++;
            break;
            case 'v':
                R[21]++;
            break;
            case 'w':
                R[22]++;
            break;
            case 'x':
                R[23]++;
            break;
            case 'y':
                R[24]++;
            break;
            case 'z':
                R[25]++;
            break;
            case 'A':
                R[26]++;
            break;
            case 'B':
                R[27]++;
            break;
            case 'C':
                R[28]++;
            break;
            case 'D':
                R[29]++;
            break;
            case 'E':
                R[30]++;
            break;
            case 'F':
                R[31]++;
            break;
            case 'G':
                R[32]++;
            break;
            case 'H':
                R[33]++;
            break;
            case 'I':
                R[34]++;
            break;
            case 'J':
                R[35]++;
            break;
            case 'K':
                R[36]++;
            break;
            case 'L':
                R[37]++;
            break;
            case 'M':
                R[38]++;
            break;
            case 'N':
                R[39]++;
            break;
            case 'O':
                R[40]++;
            break;
            case 'P':
                R[41]++;
            break;
            case 'Q':
                R[42]++;
            break;
            case 'R':
                R[43]++;
            break;
            case 'S':
                R[44]++;
            break;
            case 'T':
                R[45]++;
            break;
            case 'U':
                R[46]++;
            break;
            case 'V':
                R[47]++;
            break;
            case 'W':
                R[48]++;
            break;
            case 'X':
                R[49]++;
            break;
            case 'Y':
                R[50]++;
            break;
            case 'Z':
                R[51]++;
            break;
            case '0':
                R[52]++;
            break;
            case '1':
                R[53]++;
            break;
            case '2':
                R[54]++;
            break;
            case '3':
                R[55]++;
            break;
            case '4':
                R[56]++;
            break;
            case '5':
                R[57]++;
            break;
            case '6':
                R[58]++;
            break;
            case '7':
                R[59]++;
            break;
            case '8':
                R[60]++;
            break;
            case '9':
                R[61]++;
            break;
        }
    }

    for(int i=0;i<62;i++) if(R[i]>max) max=R[i];

    cout<<max<<endl;    

}

Output

26

Masbahul Islam
Mar 2, 2016

Bill Bell
Jul 19, 2015

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...