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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,641 questions

55,376 answers

573 users

How to remove duplicate words from free‑text in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "regexp"
    "strings"
)

/*
   splitWords

   Splits free text into words using a Unicode-aware regex.

   Go supports Unicode character classes:
     \p{L}  → any Unicode letter
     \P{L}  → any NON-letter

   So we split on ANY sequence of non-letters:
     [^\p{L}]+
*/
func splitWords(text string) []string {
    trimmed := strings.TrimSpace(text)

    // Regex: split on any sequence of non-letter characters
    re := regexp.MustCompile(`[^\p{L}]+`)
    parts := re.Split(trimmed, -1)

    return parts
}

/*
   removeDuplicateWords

   Removes duplicate words while preserving:
     - original order
     - original casing of first occurrence
     - case-insensitive comparison

   Uses a map[string]bool for O(1) lookup.
*/
func removeDuplicateWords(text string) string {
    words := splitWords(text)

    seen := make(map[string]bool)
    unique := make([]string, 0)

    for _, word := range words {
        if word == "" {
            continue
        }

        key := strings.ToLower(word) // Unicode-aware lowercase

        if !seen[key] {
            seen[key] = true
            unique = append(unique, word) // preserve original casing
        }
    }

    // Reassemble into a space-separated string
    result := strings.Join(unique, " ")
    
    return result
}

func main() {
    input := "Hello, hello! This is a test. A TEST, hello universe...   " +
        "UNIVERSE! Hello; ***  Is Anybody There?"

    output := removeDuplicateWords(input)

    fmt.Println(output)
}


/*
run:

Hello This is a test universe Anybody There

*/

 



answered 5 days ago by avibootz
...