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

55,330 answers

573 users

How to remove duplicate words with Unicode characters from free‑text in Swift

1 Answer

0 votes
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 Γεια σας

*/

 



answered 3 days ago by avibootz

Related questions

...