import Foundation
/// Removes duplicate words from a free-text string containing Unicode characters.
/// Preserves word order and the case of the first occurrence.
///
/// - Parameter input: The input string containing text and punctuation.
/// - Returns: A space-separated string of unique words.
func removeDuplicateWords(_ input: String?) -> String {
guard let input = input, !input.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
return ""
}
// 1. Correct pattern using \p{L} for letters and \p{N} for numbers across all scripts
let pattern = #"[\p{L}\p{N}_]+"#
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return ""
}
let range = NSRange(input.startIndex..<input.endIndex, in: input)
let matches = regex.matches(in: input, options: [], range: range)
// 2. Set for O(1) duplicate tracking.
var seenWords = Set<String>()
// 3. Extract matched substrings and filter duplicates sequentially.
let uniqueWords: [String] = matches.compactMap { match in
guard let matchRange = Range(match.range, in: input) else { return nil }
return String(input[matchRange])
}.filter { word in
let lowerWord = word.lowercased()
// Set.insert returns a tuple where .inserted is true if the element was new
return seenWords.insert(lowerWord).inserted
}
// 4. Join unique words with a single space.
return uniqueWords.joined(separator: " ")
}
// Main
let input = "Hello! こんにちは, ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας"
let result = removeDuplicateWords(input)
print(result)
/*
run:
Hello こんにちは Bună ziua Γεια σας
*/