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

55,449 answers

573 users

How to remove duplicate case‑insensitive words separated by multiple delimiters from a string in Scala

1 Answer

0 votes
import scala.util.matching.Regex

/*
    Remove duplicate case‑insensitive words separated by multiple delimiters.

    Features:
    - Case‑insensitive comparison (toLowerCase)
    - Preserves original casing of the first occurrence
    - Trims whitespace around tokens
    - Supports ANY number of delimiters (including multi‑character)
    - Preserves original order
    - Efficient O(n) lookup using a mutable Set
*/

/* ---------------------------------------------------------------
   Build a regex that matches ANY delimiter
--------------------------------------------------------------- */
def buildDelimiterRegex(delimiters: List[String]): Regex = {
  // Escape delimiters so characters like "|" or "*" are treated literally
  val escaped: List[String] = delimiters.map(Regex.quote)
  val pattern: String = s"(?:${escaped.mkString("|")})+"
  new Regex(pattern)
}

/* ---------------------------------------------------------------
   Split input string using multiple delimiters
--------------------------------------------------------------- */
def splitByDelimiters(input: String, delimiters: List[String]): List[String] = {
  val regex: Regex = buildDelimiterRegex(delimiters)

  regex
    .split(input)
    .map(_.trim)
    .filter(_.nonEmpty)
    .toList
}

/* ---------------------------------------------------------------
   Remove duplicates (case‑insensitive)
--------------------------------------------------------------- */
def removeDuplicatesCaseInsensitive(tokens: List[String]): List[String] = {
  val seen = scala.collection.mutable.Set[String]()
  val unique = scala.collection.mutable.ListBuffer[String]()

  for (token <- tokens) {
    val key: String = token.toLowerCase
    if (!seen.contains(key)) {
      seen += key
      unique += token
    }
  }

  unique.toList
}

/* ---------------------------------------------------------------
   Join tokens with a chosen delimiter
--------------------------------------------------------------- */
def joinTokens(tokens: List[String], delimiter: String): String =
  tokens.mkString(delimiter)

/* ---------------------------------------------------------------
   Main function
--------------------------------------------------------------- */
def removeDuplicatesMultiDelimiterCI(
    input: String,
    delimiters: List[String],
    outputDelimiter: String
): String = {
  val tokens: List[String] = splitByDelimiters(input, delimiters)
  val unique: List[String] = removeDuplicatesCaseInsensitive(tokens)
  joinTokens(unique, outputDelimiter)
}

/* ---------------------------------------------------------------
   Main
--------------------------------------------------------------- */
object Main extends App {
  val s: String =
    "AAA | aaa ,   aAA * aaA | AAa | AAA   | BBB | ccc ---- CCC | AAA ; aaa | bbb"

  val delimiters: List[String] = List("  ", "|", ",", "*", "-", ";")

  val result: String =
    removeDuplicatesMultiDelimiterCI(s, delimiters, " | ")

  println(result)
}


/*
run:

AAA | BBB | ccc

*/

 



answered Aug 2 by avibootz

Related questions

...