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
+4.2k PHP : See installed compiled-in-modules
+9k Golang : Random Rune generator
+6.9k Golang Hello World Example
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+21.6k Golang : Calculate time different
+28.1k Golang : Get time.Duration in year, month, week or day
+11.7k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+9.2k Golang : How to profile or log time spend on execution?
+4.2k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+10.5k How to tell if a binary(executable) file or web application is built with Golang?
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+31.2k Golang : How to check if a date is within certain range?