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
+30.6k Golang : Download file example
+35.4k Golang : Integer is between a range
+4.3k Linux/MacOSX : Search and delete files by extension
+11.2k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+5.8k Golang : Denco multiplexer example
+6.1k Golang : Calculate US Dollar Index (DXY)
+7.8k Golang : Grayscale Image
+7k Golang : Transform lisp or spinal case to Pascal case example
+4.9k Linux : How to set root password in Linux Mint
+12.3k Golang : "https://" not allowed in import path
+36k Golang : Save image to PNG, JPEG or GIF format.
+12.2k Golang : Get month name from date example