Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,179 questions

56,071 answers

573 users

How to find common words in two strings with Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
    "unicode"
)

/*
    Normalize a string:
    - Convert letters to lowercase
    - Replace any non-letter with a space
    This ensures consistent word comparison.
*/
func normalize(s string) string {
    var b strings.Builder
    b.Grow(len(s))

    for _, r := range s {
        if unicode.IsLetter(r) {
            b.WriteRune(unicode.ToLower(r))
        } else {
            b.WriteRune(' ')
        }
    }

    return b.String()
}

/*
    Extract words from a string.

    This function:
    - Normalizes the input
    - Splits on whitespace
    - Filters out empty entries
    - Returns a map[string]bool acting as a set for fast lookup
*/
func extractWords(s string) map[string]bool {
    normalized := normalize(s)
    parts := strings.Fields(normalized)

    words := make(map[string]bool, len(parts))
    for _, w := range parts {
        words[w] = true
    }

    return words
}

/*
    Find common words between two strings.

    This function:
    - Extracts words from both strings
    - Uses map lookups for efficiency
    - Returns a slice of common words
*/
func findCommonWords(a, b string) []string {
    wordsA := extractWords(a)
    wordsB := extractWords(b)

    var common []string
    for w := range wordsA {
        if wordsB[w] {
            common = append(common, w)
        }
    }

    return common
}

func main() {
    s1 := "The quick brown fox jumps over the lazy dog."
    s2 := "A lazy dog sleeps while the quick fox runs away."

    common := findCommonWords(s1, s2)

    fmt.Println("Common words:")
    for _, w := range common {
        fmt.Println(w)
    }
}


/*
run:

Common words:
the
quick
fox
lazy
dog

*/

 



answered 6 days ago by avibootz
...