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.

Updated on: 01-Feb-2023

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements