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

55,473 answers

573 users

How to remove duplicate case‑insensitive words separated by multiple delimiters from a string in C#

1 Answer

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

/*
    Remove duplicate case‑insensitive words separated by multiple delimiters.

    Features:
    - Case‑insensitive comparison (ToLowerInvariant)
    - Preserves original casing of first occurrence
    - Trims whitespace around tokens
    - Supports ANY number of delimiters (including multi‑character)
    - Preserves original order
    - Efficient O(n) lookup using HashSet<string>

    Algorithm:
    1. Build a regex that matches ANY delimiter.
    2. Replace all delimiters with a single sentinel.
    3. Split by that sentinel.
    4. Trim each token.
    5. Convert to lowercase for comparison.
    6. Keep only first occurrence.
    7. Reassemble using a chosen delimiter.
*/

class Program
{
    // Build a regex that matches ANY delimiter
    static string BuildDelimiterRegex(List<string> delimiters)
    {
        var escaped = new List<string>();
        foreach (var d in delimiters)
            escaped.Add(Regex.Escape(d));

        return "(" + string.Join("|", escaped) + ")";
    }

    static string RemoveDuplicatesMultiDelimiterCI(
        string input,
        List<string> delimiters,
        string outputDelimiter)
    {
        // Step 1: Build regex for all delimiters
        string regex = BuildDelimiterRegex(delimiters);

        // Step 2: Replace all delimiters with a sentinel
        string sentinel = "\n";
        string normalized = Regex.Replace(input, regex, sentinel);

        // Step 3: Split by sentinel
        string[] tokens = normalized.Split(
            new string[] { sentinel },
            StringSplitOptions.RemoveEmptyEntries);

        // Step 4: Remove duplicates (case‑insensitive)
        var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        var unique = new List<string>();

        foreach (var token in tokens)
        {
            string trimmed = token.Trim();
            if (trimmed.Length == 0) continue;

            string key = trimmed.ToLowerInvariant();

            if (!seen.Contains(key)) {
                seen.Add(key);
                unique.Add(trimmed);
            }
        }

        // Step 5: Reassemble
        return string.Join(outputDelimiter, unique);
    }

    static void Main()
    {
        string s = "AAA | aaa ,   aAA * aaA | AAa | AAA   | BBB | ccc ---- CCC | AAA ; aaa | bbb";

        var delimiters = new List<string>
        {
            "  ", "|", ",", "*", "-", ";"
        };

        string result = RemoveDuplicatesMultiDelimiterCI(s, delimiters, " | ");

        Console.WriteLine(result);
    }
}



/*
run:

AAA | BBB | ccc

*/

 



answered Aug 1 by avibootz

Related questions

...