How to remove the letters from word1 if they do not exist in word2 with Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func removeNonCommonLetters(word1, word2 string) string {
    var result strings.Builder

    for _, char := range word1 {
        if strings.ContainsRune(word2, char) {
            result.WriteRune(char)
        }
    }

    return result.String()
}

func main() {
    word1 := "forest"
    word2 := "tor"

    result := removeNonCommonLetters(word1, word2)
    fmt.Println(result)
}



/*
run:

ort

*/

 



answered Jul 10, 2025 by avibootz
...