Implementations of Hash Table
Python, Java, C++, and Ruby are just a few of the programming languages that support hash tables. They can be used as a customized data structure in addition to frequently being included in the standard library.
Example – Count characters in the String “w3wiki”.
In this example, we use a hashing technique for storing the count of the string.
#include <bits/stdc++.h>
using namespace std;
int main() {
//initialize a string
string s="w3wiki";
// Using an array to store the count of each alphabet
// by mapping the character to an index value
int arr[26]={0};
//Storing the count
for(int i=0;i<s.size();i++){
arr[s[i]-'a']++;
}
//Search the count of the character
char ch='e';
// get count
cout<<"The count of " <<ch<< " is " <<arr[ch-'a']<<endl;
return 0;
}
public class CharacterCount {
public static void main(String[] args) {
// Initialize a string
String s = "w3wiki";
// Using an array to store the count of each alphabet
// by mapping the character to an index value
int[] arr = new int[26];
// Storing the count
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a']++;
}
// Search the count of the character
char ch = 'e';
// Get count
System.out.println("The count of " + ch + " is " + arr[ch - 'a']);
}
}
# Initialize a string
s = "w3wiki"
# Using a list to store the count of each alphabet
# by mapping the character to an index value
arr = [0] * 26
# Storing the count
for i in range(len(s)):
arr[ord(s[i]) - ord('a')] += 1
# Search the count of the character
ch = 'e'
# Get count
print("The count of ", ch, " is ", arr[ord(ch) - ord('a')])
using System;
class Program {
static void Main(string[] args) {
//initialize a string
string s = "w3wiki";
// Using an array to store the count of each alphabet
// by mapping the character to an index value
int[] arr = new int[26];
//Storing the count
for (int i = 0; i < s.Length; i++) {
arr[s[i] - 'a']++;
}
//Search the count of the character
char ch = 'e';
// get count
Console.WriteLine("The count of " + ch + " is " + arr[ch - 'a']);
}
}
// Initialize a string
const s = "w3wiki";
// Using an array to store the count of each alphabet
// by mapping the character to an index value
const arr = Array(26).fill(0);
// Storing the count
for (let i = 0; i < s.length; i++) {
arr[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
}
// Search the count of the character
const ch = 'e';
// Get count
console.log(`The count of ${ch} is ${arr[ch.charCodeAt(0) - 'a'.charCodeAt(0)]}`);
Output:
The count of e is 4
Contact Us