The Distinct() Method
We use the Distinct() method to distinguish between items or variables. The LINQ library provides the Distinct method, this functionality to compare items or variables in the C# programming language as it is a query-based library. This method only removes duplicates from a single data source and returns the unique items into a new data source that would be a list. In our case, we will be using this method for the List class, so we will also add the ToList() method with the Distinct() method so that when the distinct items are recognized, they can be added to a new list.
The following is the syntax for writing this method in the C# programming language:
As can be seen, the method is used while creating a new list since it returns elements from an existing list to create a unique list. When initializing a list using this method, we must use the old list before calling the method for the inheritance of previous items of the old list.
Now that we know about the syntax, we will implement some examples and test this method with different data types of items in the C# programming language.
Example 01: Using the Distinct().ToList() Method to Remove Numbers From a List in Ubuntu 20.04
In this instance, we will be using the Distinct().ToList() method to remove numbers from an integer list in the C sharp programming language. We will first call the LINQ library, which has the Distinct().ToList() method so that it can be used further in the program. We will be transforming a list with duplicate entries and making a new list with unique values with the help of the distinct method. This method will be performed in the Ubuntu 20.04 environment.
In the prior C# program, we created an integer data type list and then used the system’s Add() function to add some items to it. We’ll make a new list and apply values to it using the “Distinct().ToList()” function, which will eliminate all of the duplicates. On the output screen, the list with the unique objects will be printed.
After compiling and executing the above program, we will get the following output as shown in this snippet below:
In the above output, we can see that all the entries of the list that were printed are unique and there are no duplicate items, and we have successfully removed duplicates from the list.
Example 02: Using the Distinct().ToList() Method to Remove Alphanumeric String From a List in Ubuntu 20.04
In this illustration, we will use the “Distinct().ToList()” method to remove duplicates from a string data type list, but the members of the list will be Alphanumeric characters to observe how the “Distinct().ToList()” method adapts. We will use the add function in the system to repeat the process of initializing a list.Library of collections. The function “Distinct().ToList()” creates a new list with unique entries. Due to its distinctiveness, the new list would then be utilized for future preference.
In the preceding C# code, we created a string data type list and then used the Add() function from the “system.collection” package to add some alphanumeric values to it. We will make a new list and apply values to it with the “Distinct().ToList()” method, which will eliminate all of the duplicates. On the output screen, the list with the unique objects will be printed.
After compiling and running the given C# code, we will get the following result, as seen in the image below:
We can see that all of the entries in the printed list are unique, and there are no duplicates, indicating that the Distinct function was effective in eliminating duplicates from the list.
After this, we will be looking into some different approaches to remove duplicates from a list in the C# programming language.
Using the Hash Set Class to Remove Duplicates in Ubuntu 20.04
In this method, we will be using the hash set class two to remove duplicates from a list using an object of the class and adding it to a new list. The hash set is a data set that only contains unique items from the “System.Collections.Generic” namespace. We will use the hash set class and create a new list in which there will be no duplicates due to the unique property of the hash set.
In the above C# program, we have initialized an integer data type list and assigned some numeric values to it. Then we created an object of the hash set class, which we then used in the value assigning of a new list so that it would have distinct values when it is printed using the display list function.
The output after compiling and executing this program is shown below:
As we can see in the output that the new list we created with the Hash set object has no duplicates as the add shared object successfully removed all the repetitive elements of the old list.
Using the IF Check to Remove Duplicates in Ubuntu 20.04
In this method, we will be using the traditional if check to verify that there are no duplicates present in the list. The if check will add only the unique items from the list and create a completely distinct list with no repetitions. We will use the foreach loop for traversing through the list to check for duplicates, not so for printing the new list with unique elements.
In the above C# program, we have initialized a string data type list and assigned some text values to it with several duplicate items. Then we started a for each loop in which we nested an if check, and we added all the unique items to a new list that we initialized before starting the for each loop. After this, we started another for each loop in which we printed all the elements of the new list. The result of this C# program will be as shown below on the output screen.
As we can see in the output screen, all the elements of the new list are unique as compared to the old list, which had several duplicates. The if check eliminated all the duplicates from the old list and added them to the new list that we saw on the output screen.
In this article, we discussed several different approaches to removing duplicate items from the list data type of the C# programming language. The different libraries of the C# language were also used in these approaches as they provided different functions and methodologies to implement this concept. The Distinct method was discussed in great detail as it is a very effective and precise method to remove duplicates from a list in the C# programming language. To eliminate duplicates from the list, we utilized the hash set class and the standard IF check. All these approaches were implemented in the Ubuntu 20.04 environment to understand the different methods better.