Hash Table

Average Rating: 4.82 (68 votes)

Aug. 17, 2016 | 22.7K views

II. Hash Table:

Using array as a simple counter

A simple array could be used as a counter. An array could be used to keep track of the frequency of each character. For example, if the input consists of ASCII characters, we could just use an integer array of size 256 to keep track of the frequency.

For example, the following program calculates each character's frequency using a simple array of size 256.

Why do we choose size 256? Why not 128 or 26? The reason is because there are a total of 256 possible ASCII characters, from 0 to 255. If you are sure that the input characters are all lowercase letters (a - z), then you can save some space by using an array of size 26:

privatestaticvoidprintFreq(char[]str){int[]freq=newint[26];for(inti=0;i<str.length;i++){// 'a' has an ascii value of 97, so there is an offset in accessing the index.freq[str[i]-'a']++;}for(inti=0;i<26;i++){if(freq[i]>0){System.out.println("["+(char)(i+'a')+"] = "+freq[i]);}}}publicstaticvoidmain(String[]args){char[]str="helloworld".toCharArray();printFreq(str);}

Output:

[d] = 1
[e] = 1
[h] = 1
[l] = 3
[o] = 2
[r] = 1
[w] = 1

Using a hash table

Now, what if the input contains unicode characters? In Java, each character is represented internally as 2 bytes, or 16 bits. That means you can increase the array size to , which would work but seems like a waste of space. For example, what if your input has only 10 characters? Most of the array elements will be initialized to 0 and to print the frequencies we need to traverse all 65536 elements one by one, which is inefficient.

A better method is to use a hash table, in Java it's called HashMap, in C++ it's called unordered_map, and in Python it's called dict.