Scala Programming: Count the number of occurrences of each element in a given list
Write a Scala program to count the number of occurrences of each element in a given list.
Sample Solution:
Scala Code:
Sample Output:
HashMap(1 -> 4, 6 -> 3, 2 -> 3, 3 -> 1, 4 -> 2) HashMap(Black -> 2, Green -> 2, Red -> 2, White -> 1)
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to check whether a list contains a sublist.
Next: Write a Scala program to split a given list into two lists.
What is the difficulty level of this exercise?