0

I have a scala map that stores a characters and their frequency in a string then I need to take the map and save all its contents into a Seq for example:

map[Char,int] = map[T,Int](a -> 3,b-> 2, c -> 1) => Seq[Char] = Seq[Char](a,a,a,b,b,c)

Anyone has any advice about how could I accomplish this or about how could I iterate through the map

SirScroll2
  • 19
  • 4

1 Answers1

1

Iterate over the Map and loop till frequency of chars and return the char for the same frequency inside the loop.

You can do something like this.

val x = Map('a' -> 3,'b' -> 2, 'c' -> 1)

x.flatMap {case(char,freq) =>
  (1 to freq).map(_ => char)
}.toSeq 
Amit Prasad
  • 725
  • 4
  • 17