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 with Unicode characters from free‑text in C#

2 Answers

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

/*
    This program removes duplicate words from free text containing Unicode characters.
    It uses:
      - C#'s built-in Unicode support
      - Lowercasing + punctuation stripping for comparison
      - Cleaned original (no punctuation, original casing) for output
      - Dictionary to preserve first occurrence order
*/

class Program
{
	// Remove punctuation but keep original casing
	static string CleanPreserveCase(string s)
	{
		var sb = new StringBuilder();
		foreach (char c in s) {
			// Keep letters, digits, and all non-ASCII Unicode characters
			if (char.IsLetterOrDigit(c) || c > 127)
				sb.Append(c);
		}

		return sb.ToString();
	}

	// Normalize a word: remove punctuation + lowercase (for comparison)
	static string NormalizeWord(string s)
	{
		var sb = new StringBuilder();
		foreach (char c in s) {
			if (char.IsLetterOrDigit(c) || c > 127)
				sb.Append(char.ToLowerInvariant(c));
		}
		
		return sb.ToString();
	}

	static void Main()
    {
        string input =
            "Hello! こんにちは,  ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας";

        // Dictionary preserves insertion order in modern .NET
        var unique = new Dictionary<string, string>();

        // Split on whitespace
        foreach (string word in input.Split((char[])null, StringSplitOptions.RemoveEmptyEntries))
        {
            string normalized = NormalizeWord(word);
            string cleaned    = CleanPreserveCase(word);

            if (normalized.Length > 0 && !unique.ContainsKey(normalized))
                unique[normalized] = cleaned;
        }

        // Print result
        var output = new StringBuilder();
        foreach (string original in unique.Values)
            output.Append(original).Append(" ");

        Console.WriteLine(output.ToString().Trim());
    }
}


/*
run:

Hello こんにちは Bună ziua Γεια σας

*/

 



answered 3 days ago by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public class WordDeduplicator
{
    /// <summary>
    /// Removes duplicate words from a free-text string containing Unicode characters.
    /// Preserves word order and the case of the first occurrence.
    /// </summary>
    /// <param name="input">The input free-text string containing punctuation and Unicode words.</param>
    /// <returns>A space-separated string of unique words.</returns>
    public static string RemoveDuplicateWords(string input)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return string.Empty;
        }

        // 1. \w+ matches any sequence of Unicode word characters (letters, digits, connector punctuation).
        //    Punctuation like !, ;, *, and spaces are automatically filtered out.
        //    Regex engine natively supports full Unicode character sets (Latin, Japanese, Greek, etc.).
        MatchCollection matches = Regex.Matches(input, @"\w+");

        // 2. Use a HashSet with OrdinalIgnoreCase to track seen words in O(1) time without extra allocations.
        var seenWords = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

        // 3. StringBuilder for efficient O(N) string construction.
        var resultBuilder = new StringBuilder();

        foreach (Match match in matches)
        {
            string word = match.Value;

            // Add returns true if the element was added (meaning it's the first time we've seen it)
            if (seenWords.Add(word))
            {
                if (resultBuilder.Length > 0)
                {
                    resultBuilder.Append(' ');
                }
                resultBuilder.Append(word);
            }
        }

        return resultBuilder.ToString();
    }

    public static void Main()
    {
        string input = "Hello! こんにちは,  ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας";
        
        string result = RemoveDuplicateWords(input);
        
        Console.WriteLine(result);
    }
}


/*
run:

Hello こんにちは Bună ziua Γεια σας

*/

 



answered 3 days ago by avibootz

Related questions

...