- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find the frequency of character in a string
String in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. The frequency of a character means the number of times a character is appearing.
Syntax
map()
To keep track of how frequently each character appears in the input string, the built-in map data structure in Go is used in example below. The map is an unsorted collection of key-value pairs with unique keys and variable types of values for the values.
func make ([] type, size, capacity)
The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.
Algorithm
Step 1 − Create a package main and declare fmt(format package) package.
Step 2 − Create a main function and in that function create a string whose character frequency is to be counted.
Step 3 − Using user-defined or internal function find the frequency of the characters in the string.
Step 4 − The output will be printed using fmt.Println() function where ln means new line.
Example 1
In this example, we will learn how to find frequency of character in a string using map. Here, map will hold the frequency of characters and the output will be a map printed with frequency of each character.
package main import ( "fmt" ) func main() { mystr := "hello alexa" //create a string fmt.Println("The string given here is:", mystr) frequency := make(map[rune]int) //create frequency map using make function for _, val := range mystr { frequency[val]++ //assign frequencies } fmt.Println("The frequency of characters in the string is:") fmt.Println(frequency) //print frequency map }
Output
The string given here is: hello alexa The frequency of characters in the string is: map[120:1 104:1 101:2 108:3 111:1 32:1 97:2]
Example 2
In this example, we will learn how to calculate frequency of character in a string using if-else statement. Basically, this conditional statement will be used to check the presence of character in the frequency map.
package main import ( "fmt" ) func main() { mystr := "hello alexa" //create string fmt.Println("The string given here is:", mystr) frequency := make(map[rune]int) //create frequency map using make function for _, val := range mystr { if _, ok := frequency[val]; ok { frequency[val]++ } else { frequency[val] = 1 //assign frequencies } } fmt.Println("The frequency of characters in the string is:") fmt.Println(frequency) //print frequency map }
Output
The string given here is: hello alexa The frequency of characters in the string is: map[97:2 120:1 104:1 101:2 108:3 111:1 32:1]
Conclusion
We executed the program of printing frequency of characters in a string using two methods. In the first method we used map in and in the second example we used if-else conditional statement along with map.
- Related Articles
- Java Program to Find the Frequency of Character in a String
- C++ Program to Find the Frequency of a Character in a String
- Java program to find the Frequency of a character in a given String
- Golang Program to Convert Character to String
- Golang program to replace the spaces of string with a specific character
- Golang program to capitalize first character of each word in a string
- Golang program to iterate through each character of string
- Swift Program to Find the Frequency of Characters in a String
- Golang Program to find the length of a string
- Write a Golang program to find the frequency of an element in an array
- Write a Golang program to find the frequency of each element in an array
- C program to find frequency of each digit in a string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Frequency of each character in String in Python
- C Program to find maximum occurrence of character in a string
