Dart Cookbook: Merge 2 lists and get unique values
The first challenge in this Dart Cookbook series!
While trying to learn Dart I came up with the question "How can I get familiar with the language if I already know other programming languages?".
To solve this conflict, I choose to solve some programming challenges instead of just reading all the documentation. Don't get me wrong, Dart has an excellent documentation that I encourage you to read, but learning only using the docs sounds a bit boring.
This is the first post of my Dart Cookbook series. Keep tuned to get all the new posts!
Challenge: Merge 2 lists of strings and only keep the unique values.
Step 1: Create a main function
Dart requires a main function to run your code. Use the syntax below.
void main() {
}
Step 2: Create the lists
Thanks to Dart type interference we can create variables with the keyword var
and our program will know that we are providing a list of strings during runtime.
var listA = ['red', 'blue', 'green', 'white'];
var listB = ['yellow', 'blue', 'black', 'white'];
Step 3: Defining our function
We can define a function in Dart with the following syntax
<return type> functionName(<param 1 type> paramName1, …) {
}
For this example, we will merge 2 lists of strings and to get a list containing only the unique values. We can achieve this with the class List<String>
.
List<String> mergeUnique(List<String> listA, List<String> listB) {
}
Step 4: Add our merging and filtering logic.
Let's solve this in 2 steps: Create a list that contains all the elements of the 2 lists, including duplicates. Filter the duplicates.
When creating the result list, we can tell Dart to take the elements of an existing list using the constructor List.from()
. In this case we will start with the elements of listA
.
List<String> result = new List.from(listA);
And then add the elements of listB
with the method addAll()
.
result.addAll(listB);
Now, we need to filter this result list. To achieve this we can use a Set
. A Set
is a collection of objects in which each object can only appear once in the collection.
Using the method toSet()
will get rid of all the duplicates. And then we can apply the method toList()
to keep our collection as a list.
return result.toSet().toList();
At the end, we should finish with this function:
List<String> mergeUnique(List<String> listA, List<String> listB) {
List<String> result = new List.from(listA);
result.addAll(listB);
return result.toSet().toList();
}
Step 5 Show our results
Let's show our results in the terminal. For this we can use the print()
inside of our main function and pass the function that we created as a parameter.
void main() {
var listA = ['red', 'blue', 'green', 'white'];
var listB = ['yellow', 'blue', 'black', 'white'];
print(mergeUnique(listA, listB));
}
Complete code
The complete code should look like this:
void main() {
var listA = ['red', 'blue', 'green', 'white'];
var listB = ['yellow', 'blue', 'black', 'white'];
print(mergeUnique(listA, listB));
}
///Merges the values of [listA] and [listB]
///and keeps only the unique values.
List<String> mergeUnique(List<String> listA, List<String> listB) {
List<String> result = new List.from(listA);
result.addAll(listB);
return result.toSet().toList();
}
Results
Let’s run it with the command dart run <filename>
. This is the result we should get:
[red, blue, green, white, yellow, black]
⬇️ Download
All my projects are available on my GitHub profile, feel free to download them!
Get the Dart Cookbook repository
💬 Comments
If you have any recommendations feel free to comment. I appreciate any feedback!
🔗 References
List of resources that I consulted and that may be helpful to go deeper on this topic.