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
+11.4k Golang : How to determine if user agent is a mobile device example
+6.2k Golang : Number guessing game with user input verification example
+14.7k Golang : Get input from keyboard
+4.2k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+10.9k Golang : Generate Code128 barcode
+29.7k Golang : Convert []string to []byte examples
+4.5k Golang : Map within a map example
+12.6k Golang : Get current time from the Internet time server(ntp) example
+15.6k Convert JSON to CSV in Golang
+13.9k Golang : How to extract links from web page ?
+7.7k Facebook : Getting the friends list with PHP return JSON format
+23.9k Golang : Calculate future date with time.Add() function