Remove duplicates from a given list
Scala Programming List Exercise-7 with Solution
Write a Scala program to remove duplicates from a given list.
Sample Solution:
Scala Code:
object Scala_List
{
def main(args: Array[String]): Unit =
{
val nums = List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3)
println("Original list:")
println(nums)
val result1 = nums.distinct
println("Unique elements of the said list:")
println(result1)
val chars = List("a", "a", "b", "c", "d", "c", "e", "f")
println("Original list:")
println(chars)
val result2 = chars.distinct
println("Unique elements of the said list:")
println(result2)
}
}
Sample Output:
Original list: List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3) Unique elements of the said list: List(1, 3, 5, 2, 7, 9, 11, 14, 12) Original list: List(a, a, b, c, d, c, e, f) Unique elements of the said list: List(a, b, c, d, e, f)
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to find the largest and smallest number from a given list.
Next: Write a Scala program to check a given list is empty or not.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework