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
+16.7k Golang : Get host name or domain name from IP address
+4.2k Python : Print unicode escape characters and string
+11.4k Golang : Generate Code128 barcode
+15.9k Golang : Get download file size
+15.1k Golang : Aligning strings to right, left and center with fill example
+8.8k Golang : How to profile or log time spend on execution?
+5.6k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+6.2k Golang : Grayscale Image
+6.1k Setting $GOPATH environment variable for Unix/Linux and Windows
+14.2k Golang : Read a file line by line
+67.8k Golang : How to return HTTP status code?