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
+24.6k Golang : How to write CSV data to file
+4.8k Fix yum-complete-transaction error
+24.3k Golang : How to read integer value from standard input ?
+4.1k HTTP common errors and their meaning explained
+2.1k Golang : Fix go-cron set time not working issue
+16.5k Golang : Read data from config file and assign to variables
+5.2k Golang : Scan forex opportunities by Bollinger bands
+9.9k Golang : Removes punctuation or defined delimiter from the user's input
+8.4k Golang : Changing a RGBA image number of channels with OpenCV
+11.1k Android Studio : Highlight ImageButton when pressed on example
+9.6k Golang : Create matrix with Gonum Matrix package example
+5.1k Linux/MacOSX : Search for files by filename and extension with find command