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
*/