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
+7.7k Golang : What fmt.Println() can do and println() cannot do
+8k Swift : Convert (cast) Character to Integer?
+5.4k Golang : Shortening import identifier
+12.8k Golang : How to calculate the distance between two coordinates using Haversine formula
+7.8k Golang : Multiplexer with net/http and map
+10k Golang : Embed secret text string into binary(executable) file
+5.9k Golang : Get missing location after unmarshal binary and gob decode time.
+22.7k Golang : Read a file into an array or slice example
+9k Golang : Temperatures conversion example
+51.1k Golang : Check if item is in slice/array
+9.2k Golang : Changing a RGBA image number of channels with OpenCV
+25k Golang : Convert uint value to string type