To find the characters in the first string that are not present in the second string in Go, you can use loops and maps.
In this example,
str1
and str2
with different values.str2
.str1
and check if they are present in the map. If not, we append them to the result string.// Find Characters Not Present in Second String
package main
import (
"fmt"
)
func main() {
str1 := "Hello World"
str2 := "apple"
bytes1 := []byte(str1)
bytes2 := []byte(str2)
freqMap := make(map[byte]int)
for _, char := range bytes2 {
freqMap[char]++
}
var result []byte
for _, char := range bytes1 {
if freqMap[char] == 0 {
result = append(result, char)
}
}
fmt.Printf("Characters not present in second string: %s\n", result)
}
Characters not present in second string: Ho Word
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Go language with well detailed examples.