Open In App

How to remove duplicate characters from a String in Scala?

In this article, we will learn how to remove duplicate characters from a string in Scala using different approaches. First, we will look through the Brute-Force Approach and then use different standard libraries.

Examples:

Input: String = "hello"
Output: helo

Input: String = "Geeks"
Output: Geks

Naive Approach to remove duplicate characters from a String in Scala

In the brute force approach, we compare each character in the string with every other character to determine if it's a duplicate.

Below is the code in Scala.

// Program to remove duplicate from string using scala
// Creating Object
object Gfg {
  //Method to removeDuplicates
  def removeDuplicates(str: String): String = {
    var result = ""
    for (char <- str) {
      if (!result.contains(char)) {
        result += char
      }
    }
    result
  }
   // Main Method
  def main(args: Array[String]): Unit = {
    val inputString = "hello"
    val outputString = removeDuplicates(inputString)
    println(stringWithoutDuplicates) 
  }
}

Output


Screenshot-(297)

Output

Explanation:

Time Complexity : O(n2)

Remove duplicate characters from a String in Scala Using Set

This is the another approach using a mutable Set to keep track of duplicate characters.

Below is the code in Scala using set.

// Program to remove duplicate from string using set

//creating object
object Gfg {
  
  //remove duplicate function
  def removeDuplicates(str: String): String = {
    val set = scala.collection.mutable.Set[Char]()
    val result = new StringBuilder
    for (char <- str) {
      if (!set(char)) {
        result += char
        contain += char
      }
    }
    result.toString
  }

  //Main Method
  def main(args: Array[String]): Unit = {
    val inputString = "hello"
    val outputString = removeDuplicates(inputString)
    println(outputString) 
  }
}

Output:

Screenshot-(297)

Output

Explanation:

Time Complexity: O(n)
Auxiliary Space: O(n)

Remove duplicate characters from a String in Scala Using foldLeft Method

This is the another method for removing duplicate characters from a String in Scala.

Below is the code in Scala.

//Creating Object
object Gfg {
  
  // defining removeDuplicates Method
  def removeDuplicates(str: String): String = {
    
  // Use foldLeft to iterate through each character of the string
    str.foldLeft("") { (acc, char) =>
     // If the accumulator already contains the character, return the accumulator
      if (acc.contains(char)) acc
      // Otherwise, append the character to the accumulator
      else acc + char
    }
  }
  
 // Defining Main Method
  def main(args: Array[String]): Unit = {
    val inputString = "Geeksforgeeks"
    val outputString = removeDuplicates(inputString)
    println(outputString)
  }
}

Output:

Screenshot-(298)

Output

Explanation:

Remove duplicate characters from a String in Scala Using Distinct Method

The distinct method provides a convenient way to remove duplicate characters from a string in Scala.

Below is the code of remove duplicate characters from a string using string.distinct Method.

// Creating Object
object Gfg {
  // defining a removeDuplicate Method
  def removeDuplicates(str: String): String = {
    // Using string.distinct method
    str.distinct
  }

  //defining main Method
  def main(args: Array[String]): Unit = {
    val inputString = "Geeksforgeeks"
    val outputString = removeDuplicates(inputString)
    println(outputString) 
  }
}

Output:

Screenshot-(298)

Output

Explanation

Time Complexity: O(n)
Auxiliary Space: O(n)

Article Tags :