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

55,678 answers

573 users

How to remove duplicate words from free‑text in Scala

1 Answer

0 votes
import scala.collection.mutable

object RemoveDuplicateWordsFreeText {

  /*
     splitWords

     Splits free text into words using a Unicode-aware regex.

     We use String.split instead of Regex.split because:
       - It supports Unicode properties like \p{L}
       - It avoids Scala's Regex.split deprecation warnings
       - It is efficient and idiomatic

     Regex:
       [^\p{L}]+   → any sequence of NON-letter characters
  */
  def splitWords(text: String): Seq[String] = {
    val trimmed: String = text.trim

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

    parts.filter(_.nonEmpty)
  }

  /*
     removeDuplicateWords

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

     Uses mutable.HashSet for O(1) lookup.
  */
  def removeDuplicateWords(text: String): String = {
    val words: Seq[String] = splitWords(text)

    val seen: mutable.HashSet[String] = mutable.HashSet.empty[String]
    val unique: mutable.ListBuffer[String] = mutable.ListBuffer.empty[String]

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

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

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

  def main(args: Array[String]): Unit = {
    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
...