How to Merge Two Lists and Remove Duplicates in Scala?
Last Updated :
26 Mar, 2024
In Scala, lists are flexible data structures that are crucial for many programming tasks. They help in organizing and handling collections of items effectively. Knowing how to work with lists efficiently is vital for anyone coding in Scala. In this article, we'll explore how to combine two lists and get rid of any repeating items. We'll cover these topics thoroughly, providing clear explanations and practical examples to help you grasp these concepts easily.
What is a List in Scala?
A list is an ordered collection of elements of the same type. Unlike arrays, lists are immutable, meaning that once created, their elements cannot be modified. Lists are created using the List class in Scala, and they support various operations such as appending elements, accessing elements by index, and more.
Below is the Scala program to implement the List:
Scala
object HelloWorld {
// Main method
def main(args: Array[String]) {
// Define a list of integers
val numbers: List[Int] = List(1, 2, 3, 4, 5);
// Define a list of strings
val fruits: List[String] = List("Apple", "Banana", "Orange", "Grape", "Pineapple");
// Define an empty list
val emptyList: List[Int] = List();
// Accessing elements of a list
println("First element of numbers list: " + numbers.headOption.getOrElse("List is empty")); // Output: 1
println("Last element of fruits list: " + fruits.lastOption.getOrElse("List is empty")); // Output: Pineapple
// Adding elements to a list
val updatedNumbers = numbers :+ 6; // Add element at the end
val updatedFruits = "Mango" :: fruits; // Add element at the beginning
// Display the updated lists
println("Updated numbers list: " + updatedNumbers); // Output: List(1, 2, 3, 4, 5, 6)
println("Updated fruits list: " + updatedFruits); // Output: List(Mango, Apple, Banana, Orange, Grape, Pineapple)
}
}
Output:

Explanation:
- A list of integers numbers and a list of strings fruits are defined.
- An empty list emptyList is also defined.
- To access the elements of a list the head and last methods are used.
- The elements are added to the list using :+ (append) and :: (prepend) operators.
- The updated lists is displayed to observe the changes.
Merging Two Lists in Scala
Merging two lists involves combining their elements to form a single list. In Scala, you can merge two lists using the ++ operator or the ::: method. Below is the Scala program to implement both the approaches:
Scala
object HelloWorld {
def main(args: Array[String]) {
// Define two lists
val list1 = List(1, 2, 3)
val list2 = List(4, 5, 6)
// Method 1: Using the ++ operator
val mergedList1 = list1 ++ list2
// Method 2: Using the ::: method
val mergedList2 = list1 ::: list2
// Display the merged lists
println("Merged List (Using ++ operator): " + mergedList1)
println("Merged List (Using ::: method): " + mergedList2)
}
}
Output:

Implementation of Removing Duplicates from a List
Removing duplicates from a list involves eliminating duplicate elements to ensure each element appears only once in the resulting list. In Scala, you can achieve this by converting the list to a set and then back to a list, or by using the distinct method. Below is the Scala program to implement both the approaches:
Scala
object HelloWorld {
def main(args: Array[String]) {
// Define a list with duplicate elements
val listWithDuplicates = List(1, 2, 3, 2, 4, 3, 5);
// Method 1: Using conversion to set and back to list
val uniqueList1 = listWithDuplicates.toSet.toList;
// Method 2: Using the distinct method
val uniqueList2 = listWithDuplicates.distinct;
// Display the unique lists
println("Unique List (Using set conversion): " + uniqueList1);
println("Unique List (Using distinct method): " + uniqueList2);
}
}
Output:

Similar Reads
How to Merge Two Arrays and Remove Values that have Duplicates? In this article, we will discuss how to merge two arrays and remove values that have duplicate Ruby. We can merge two arrays and remove values that have duplicates through various methods provided in Ruby. Table of Content Using the | OperatorUsing the uniq MethodUsing the Concat with uniq MethodUsi
2 min read
How to remove duplicate characters from a String in Scala? In this article, we will learn how to remove duplicate characters from a string in Scala using different approaches. First, we will look through the Brute-Force Approach and then use different standard libraries. Examples: Input: String = "hello"Output: helo Input: String = "Geeks"Output: Geks Naive
4 min read
How to Reduce Code Duplication in Scala? For this definition, duplicate code refers to a sequence of source code that appears more than once in a program, whether inside the same program or across various programs owned or maintained by the same company. For a variety of reasons, duplicate code is typically seen as bad. A minimal requireme
8 min read
How to Join Two DataFrame in Scala? Scala stands for scalable language. It is a statically typed language although unlike other statically typed languages like C, C++, or Java, it doesn't require type information while writing the code. The type verification is done at the compile time. Static typing allows us to build safe systems by
4 min read
How to Sort a list in Scala? Sorting a list is a common operation in programming, and Scala provides convenient ways to accomplish this task. In this article, we'll explore different methods to sort a list in Scala, along with examples. Table of Content Using the sorted Method:Using the sortBy Method:Using the sortWith Method:S
2 min read