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,690 questions

55,449 answers

573 users

How to remove duplicate words from free‑text in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text.RegularExpressions
Imports System.Collections.Generic

Module RemoveDuplicateWordsFreeText

    '---------------------------------------------------------------
    ' Splits free text into words using Unicode-aware regex.
    '
    ' Regex explanation:
    '   \P{L}+   → any sequence of NON-letter characters
    '   \p{L}    → any Unicode letter (Hebrew, Arabic, Latin, etc.)
    '
    ' This gives correct splitting for multilingual free text.
    '---------------------------------------------------------------
    Function SplitWords(text As String) As List(Of String)
        text = text.Trim()

        ' Unicode-aware split on non-letter sequences
        Dim parts As String() = Regex.Split(text, "\P{L}+")
        Return New List(Of String)(parts)
    End Function


    '---------------------------------------------------------------
    ' Removes duplicate words while preserving:
    '   - original order
    '   - original casing of first occurrence
    '   - case-insensitive comparison
    '
    ' Uses HashSet for O(1) average lookup time.
    '---------------------------------------------------------------
    Function RemoveDuplicateWords(text As String) As String
        Dim words As List(Of String) = SplitWords(text)

        Dim seen As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
        Dim unique As New List(Of String)()

        For Each word In words
            If word = "" Then Continue For

            ' Case-insensitive check via HashSet
            If Not seen.Contains(word) Then
                seen.Add(word)
                unique.Add(word)   ' preserve original casing
            End If
        Next

        ' Reassemble into a space-separated string
        Return String.Join(" ", unique)
    End Function


    '---------------------------------------------------------------
    ' Program entry point
    '---------------------------------------------------------------
    Sub Main()
        Dim input As String =
            "Hello, hello! This is a test. A TEST, hello universe...   " &
            "UNIVERSE! Hello; ***  Is Anybody There?"

        Dim output As String = RemoveDuplicateWords(input)

        Console.WriteLine(output)
    End Sub

End Module



' run:
'
' Hello This is a test universe Anybody There
'

 



answered Aug 2 by avibootz
...