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 from free‑text in Swift

1 Answer

0 votes
import Foundation

/*
    splitWords

    Splits free text into words using Unicode-aware logic.

    Swift's CharacterSet.letters includes ALL Unicode letters.
    So we split on ANY sequence of NON-letter characters by using
    components(separatedBy:) with the inverted set.

    This is fully Unicode-aware and avoids regex portability issues.
*/
func splitWords(_ text: String) -> [String] {
    let trimmed: String = text.trimmingCharacters(in: .whitespacesAndNewlines)

    // Split on any sequence of non-letter characters
    let parts: [String] = trimmed
        .components(separatedBy: CharacterSet.letters.inverted)
        .filter { !$0.isEmpty }

    return parts
}

/*
    removeDuplicateWords

    Removes duplicate words while preserving:
      - original order
      - original casing of first occurrence
      - case-insensitive comparison

    Uses Set<String> for O(1) lookup.
*/
func removeDuplicateWords(_ text: String) -> String {
    let words: [String] = splitWords(text)

    var seen: Set<String> = Set()
    var unique: [String] = []

    for word in words {
        let key: String = word.lowercased()   // Unicode-aware lowercase

        if !seen.contains(key) {
            seen.insert(key)
            unique.append(word)               // preserve original casing
        }
    }

    // Reassemble into a space-separated string
    return unique.joined(separator: " ")
}

// ------------------------------------------------------------
// Program entry point
// ------------------------------------------------------------
let input: String =
    "Hello, hello! This is a test. A TEST, hello universe...   " +
    "UNIVERSE! Hello; ***  Is Anybody There?"

let output: String = removeDuplicateWords(input)

print(output)



/*
run:

Hello This is a test universe Anybody There

*/

 



answered 4 days ago by avibootz
...