How to remove duplicate words from free‑text in Kotlin

1 Answer

0 votes
/*
    A clean, idiomatic Kotlin program that removes duplicate words
    from free-text while preserving original order and casing.
*/

fun splitWords(text: String): List<String> {
    /*
        Splits free text into words using a Unicode-aware regex.

        Kotlin/Java regex supports Unicode properties:
          \p{L}      → any Unicode letter
          [^\p{L}]+  → any sequence of NON-letters

        This gives correct splitting for multilingual free text.
    */

    val trimmed: String = text.trim()

    // Split on any sequence of non-letter characters
    val parts: List<String> = trimmed.split(Regex("[^\\p{L}]+"))

    // Filter out empty tokens
    return parts.filter { it.isNotEmpty() }
}

fun removeDuplicateWords(text: String): String {
    /*
        Removes duplicate words while preserving:
          - original order
          - original casing of first occurrence
          - case-insensitive comparison

        Uses HashSet<String> for O(1) lookup.
    */

    val words: List<String> = splitWords(text)

    val seen: HashSet<String> = HashSet()
    val unique: MutableList<String> = mutableListOf()

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

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

    // Reassemble into a space-separated string
    return unique.joinToString(" ")
}

fun main() {
    val input: String =
        "Hello, hello! This is a test. A TEST, hello universe...   " +
        "UNIVERSE! Hello; ***  Is Anybody There?"

    val output: String = removeDuplicateWords(input)

    println(output)
}


/*
run:

Hello This is a test universe Anybody There

*/

 



answered Aug 3 by avibootz
...