/**
* Removes duplicate words from a free-text string containing Unicode characters.
* Preserves word order and the case of the first occurrence.
*
* @param {string} input - The input string containing text and punctuation.
* @returns {string} Space-separated unique words.
*/
function removeDuplicateWords(input) {
if (!input || !input.trim()) {
return '';
}
// 1. Unicode property escape pattern (\p{L} = letters, \p{N} = numbers).
// 'g' flag finds all matches, 'u' enables full Unicode character support.
const wordPattern = /[\p{L}\p{N}_]+/gu;
const matches = input.match(wordPattern) || [];
// 2. Set for O(1) duplicate tracking.
const seen = new Set();
const uniqueWords = [];
// 3. Keep first occurrence while ignoring case.
for (const word of matches) {
const lowerWord = word.toLowerCase();
if (!seen.has(lowerWord)) {
seen.add(lowerWord);
uniqueWords.push(word);
}
}
// 4. Join unique words with a single space.
return uniqueWords.join(' ');
}
// Main
const input = "Hello! こんにちは, ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας";
const result = removeDuplicateWords(input);
console.log(result);
/*
run:
Hello こんにちは Bună ziua Γεια σας
*/