THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
< image src=“golang/golang-example.png” alt=“Golang Program to check Duplicate characters” title=“Golang Example String contains Duplicate characters”>}}
In this blog post, We are going to learn ways to check duplicate characters from a string.
String
is a group of any characters which are called a group of runes
datatypes in Golang
. It contains any characters, but sometimes duplicate characters also.
We are going to learn the below things with duplicate characters in a string.
In this example, find the count of repeated characters using the String.count() function.
Following are the steps for the program.
package main
import (
"fmt"
"strings"
)
func main() {
strText := "abc aef bbbbbbbb"
count := strings.Count(strText, "b")
fmt.Printf("Duplicate character b count in [%v] is %d ", strText, count)
}
Output is
Duplicate character b count in [abc aef bbbbbbbb] is 9
To remove duplicate characters from a string, follow the below steps
rune
typeString.Builder
.package main
import (
"fmt"
"strings"
)
func main() {
value := "Hello"
var strBuilder strings.Builder
var character rune
for index, currentChar := range value {
if currentChar != character || index == 0 {
strBuilder.WriteRune(r)
character = currentChar
} else {
continue
}
}
fmt.Println(strBuilder.String())
}
Output:
helo
đź§® Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts