Question: Using pattern matching, write a function using SCALA that computes for each unique character in the list `chars` the number of times it occurs: def

Using pattern matching, write a function using SCALA that computes for each unique character in the list `chars` the number of times it occurs:

def times(chars: List[Char]): List[(Char, Int)] = ??? For example, the invocation

times(List('a', 'b', 'a'))

should return the following (the order of the resulting list is not important):

List(('a', 2), ('b', 1))

Help on Pairs The type `List[(Char, Int)]` denotes a list of pairs, where each pair consists of a character and an integer. Pairs can be constructed easily using parentheses: val pair: (Char, Int) = ('c', 1) In order to access the two elements of a pair, you can use the accessors `_1` and `_2`: val theChar = pair._1

val theInt = pair._2 Another way to deconstruct a pair is using pattern matching: pair match { case (theChar, theInt) => println("character is: "+ theChar) println("integer is : "+ theInt) }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!