Skip to main content
Code Review

Return to Question

deleted 7 characters in body; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Find first Repeatingrepeating Char in String

Given a string, find the first repeating character in it.

ExampleExamples:

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)
  • firstUnique("Vikrant")None
  • firstUnique("VikrantVikrant")Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

Find first Repeating Char in String

Given a string, find the first repeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

Find first repeating Char in String

Given a string, find the first repeating character in it.

Examples:

  • firstUnique("Vikrant")None
  • firstUnique("VikrantVikrant")Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

fixed problem statement.
Source Link
vikrant
  • 405
  • 2
  • 8

Find first UniqueRepeating Char in String

Given a string, find the first non-repeatingrepeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

Find first Unique Char in String

Given a string, find the first non-repeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

Find first Repeating Char in String

Given a string, find the first repeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

added 92 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Find first Unique Char in String (Scala)

Given a string, find the first non-repeating character in it.

example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Given a string, find the first non-repeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Here is scalaScala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}
object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion.? Instead of using checkOccurencethe checkOccurence method I can traverse through the string anand breakbreak when I find the first element with a count more than 1. But But that will require a break, which is discouraged in Scala.

Find first Unique Char in String (Scala)

Given a string, find the first non-repeating character in it.

example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Here is scala implementation

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion. Instead of using checkOccurence method I can traverse through the string an break when I find first element with count more than 1. But that will require a break, which is discouraged in Scala.

Find first Unique Char in String

Given a string, find the first non-repeating character in it.

Example

i/p - Vikrant
o/p - None
i/p - VikrantVikrant
o/p - Some(V)

Scala implementation:

object FirstUniqueChar extends App {
 def firstUnique(s: String): Option[Char] = {
 val countMap = (s groupBy (c=>c)) mapValues(_.length)
 def checkOccurence(s1: String ): Option[Char] = {
 if (countMap(s1.head) > 1) Some(s1.head)
 else if (s1.length == 1) None
 else checkOccurence(s1.tail)
 }
 checkOccurence(s)
 }
 println(firstUnique("abcdebC"))
 println(firstUnique("abcdef"))
}

I also have a followup question. What is the recommended way if I do not want to solve this problem with recursion? Instead of using the checkOccurence method I can traverse through the string and break when I find the first element with a count more than 1. But that will require a break, which is discouraged in Scala.

Source Link
vikrant
  • 405
  • 2
  • 8
Loading
lang-scala

AltStyle によって変換されたページ (->オリジナル) /