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
+39.7k Golang : How to check if a string contains another sub-string?
+6.9k Golang : Configure Apache and NGINX to access your Go service example
+35.6k Upload multiple files with Go
+9.7k Golang : Simple image viewer with Go-GTK
+4.8k Golang : Use NLP to get sentences for each paragraph example
+5.9k Golang : Takes a plural word and makes it singular
+15.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+10k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+16.3k Golang : [json: cannot unmarshal object into Go value of type]
+10.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+7.5k Golang : How to capture return values from goroutines?
+10.3k Golang : How to flush a channel before the end of program?