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
+29.2k Golang : Saving(serializing) and reading file with GOB
+13.3k Golang : Generate Code128 barcode
+9.6k Golang : Eroding and dilating image with OpenCV example
+7.7k Swift : Convert (cast) String to Double
+9.9k Golang : Channels and buffered channels examples
+11.7k Golang : convert(cast) float to string
+20.6k Golang : Saving private and public key to files
+6.1k Linux/Unix : Commands that you need to be careful about
+7.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+14.8k Golang : How to check for empty array string or string?
+14.9k Golang : Search folders for file recursively with wildcard support
+6.7k Golang : Get expvar(export variables) to work with multiplexer