Golang : How to count the number of repeated characters in a string?
Problem :
You want to develop a new algorithm that somehow will reduce or compress the size of a string significantly. In order to do that you need to determine how many characters are repeated in a string. How to count the number of repeated characters?
Solution :
Use the strings.Count()
function to find out the number of repeated characters.
For example :
package main
import (
"fmt"
"strings"
)
func main() {
str := "a long string with many repeated characters"
numberOfa := strings.Count(str, "a")
fmt.Printf("[%v] string has %d of characters of [a] ", str, numberOfa)
}
Output :
[a long string with many repeated characters] string has 5 of characters of [a]
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.6k Golang : Secure(TLS) connection between server and client
+6.5k Golang : Map within a map example
+19.2k Golang : Check if directory exist and create if does not exist
+15.3k Golang : Get query string value on a POST request
+7.2k Golang : Check if one string(rune) is permutation of another string(rune)
+11.5k Golang : Generate DSA private, public key and PEM files example
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+13.5k Golang : How to get year, month and day?
+5.5k Golang : Display advertisement images or strings on random order
+10.7k Golang : Get currencies exchange rates example
+21.8k Golang : Convert string slice to struct and access with reflect example
+7.5k Golang : Dealing with struct's private part