/**
* splitWords
*
* Splits free text into words using a Unicode-aware regex.
*
* JavaScript/TypeScript supports Unicode property escapes with the "u" flag:
*
* \p{Letter} → any Unicode letter
*
* So we split on ANY sequence of NON-letters:
*
* /[^\p{Letter}]+/u
*
* This is fully Unicode-aware and works in modern JS engines.
*/
function splitWords(text: string): string[] {
const trimmed: string = text.trim();
// Split on any sequence of non-letter characters
const parts: string[] = trimmed.split(/[^\p{Letter}]+/u);
return parts;
}
/**
* removeDuplicateWords
*
* Removes duplicate words while preserving:
* - original order
* - original casing of first occurrence
* - case-insensitive comparison
*
* Uses Set for O(1) average lookup time.
*/
function removeDuplicateWords(text: string): string {
const words: string[] = splitWords(text);
const seen: Set<string> = new Set<string>();
const unique: string[] = [];
for (const word of words) {
if (word === "") continue;
const key: string = word.toLowerCase(); // Unicode-aware lowercase
if (!seen.has(key)) {
seen.add(key);
unique.push(word); // preserve original casing
}
}
// Reassemble into a space-separated string
const result: string = unique.join(" ");
return result;
}
// ------------------------------------------------------------
// Program entry point
// ------------------------------------------------------------
const input: string =
"Hello, hello! This is a test. A TEST, hello universe... " +
"UNIVERSE! Hello; *** Is Anybody There?";
const output: string = removeDuplicateWords(input);
console.log(output);
/*
run:
Hello This is a test universe Anybody There
*/