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

55,330 answers

573 users

How to remove duplicate words from free‑text in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class RemoveDuplicateWordsFreeText
{
    /*
     * SplitWords
     *
     * 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, Cyrillic, etc.)
     *
     * This gives correct splitting for multilingual free text.
     */
    static List<string> SplitWords(string text)
    {
        text = text.Trim();

        // Unicode-aware split on non-letter sequences
        string[] parts = Regex.Split(text, @"\P{L}+");

        return new List<string>(parts);
    }

    /*
     * RemoveDuplicateWords
     *
     * Removes duplicate words while preserving:
     *   - original order
     *   - original casing of first occurrence
     *   - case-insensitive comparison
     *
     * Uses HashSet for O(1) average lookup time.
     */
    static string RemoveDuplicateWords(string text)
    {
        List<string> words = SplitWords(text);

        HashSet<string> seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        List<string> unique = new List<string>();

        foreach (string word in words)
        {
            if (word == "") continue;

            // Case-insensitive check via HashSet
            if (!seen.Contains(word)) {
                seen.Add(word);
                unique.Add(word);   // preserve original casing
            }
        }

        // Reassemble into a space-separated string
        return string.Join(" ", unique);
    }

    static void Main()
    {
        string input =
            "Hello, hello! This is a test. A TEST, hello universe...   " +
            "UNIVERSE! Hello; ***  Is Anybody There?";

        string output = RemoveDuplicateWords(input);

        Console.WriteLine(output);
    }
}



/*
run:

Hello This is a test universe Anybody There

*/

 



answered 5 days ago by avibootz
...