Updated March 17, 2023
Introduction to List and Set in C#
List and Set are two of the many data structures supported by C#. The list is an abstract data structure that is linear. It comprises of elements arranged in a linear way. It is possible to add elements to the list at different positions. The list has a property called length (number of elements in the list). In this topic, we are going to learn about List vs Set.
Set is an abstract data structure. Set has unique elements, and the order of elements is not important. Elements in the set cannot be accessed via indices. The elements themselves are indices in the set, and a particular element can be accessed using a foreach loop.
Head to Head Comparison Between List and Set (Infographics)
Below are the top 5 differences between List vs Set
Key Differences Between List and Set
Following are the key differences:
- The list can contain duplicate elements while set cannot, As a set can have only unique elements. So use a set if you want unique elements.
- Elements in the list are ordered while the order is not important in a set, but it depends on the set’s implementation.
- Elements are accessed using indices in the list, while elements themselves are indices in the set. Hence foreach loop is used to access elements of the set. However, it can also be used to access the list elements, as shown in the code of the list above.
Let us look at some of the methods of the interfaces of the list and set.
List
Following are some of the methods implemented by the IList interface:
- int Add(element) – to append the element to the list (at the end) and to return a value indicating successful insertion.
- void Insert(int, element) – to insert the element at the given position in the list
- void Clear() – to remove all elements from the list
- bool Contains(element) – to check if the element is present in the list
- void Remove(element) – to remove the given element from the list
- void RemoveAt(int) – to remove the element at the given position
- int IndexOf(element) – to return the position of the element
- this[int] – it is an indexer which allows access to the element on the given position
Following is one of the example code for the list:
// C# Program to remove the element at // the specified index of the List<T> using System; using System.Collections.Generic;
class StaticList {
// Main Method public static void Main(String[] args)
{
// Creating an List<T> of Integers
List<int> firstlist = new List<int>();
// Adding elements to List firstlist.Add(17); firstlist.Add(19); firstlist.Add(21); firstlist.Add(9); firstlist.Add(75); firstlist.Add(19); firstlist.Add(73);
Console.WriteLine("Elements Present in the List:");
// Displaying the elements of List for(int k=0;k<firstlist.Count;k++)
{
Console.Write(firstlist[k]+" ");
}
Console.WriteLine(" ");
// removing the element at index 3
Console.WriteLine("Removing the element at index 3");
// 9 will be removed from the List // and 75 will come at index 3 firstlist.RemoveAt(3);
// Displaying the elements of List foreach(int k in firstlist)
{
Console.Write(k+" ");
}
}
}
Output:
Set
Following are some of the methods implemented by the ISet interface:
- bool Add(element) – Returns true if the element is added to the set provided it is not present in the set else returns false
- bool Contains(element) – Returns true if the element is already present in the set, else it returns false
- bool Remove(element) – Returns true if the element exists in the set and can be removed; else returns false
- void Clear() – To remove all elements from the set
- void IntersectWith(Set other) – It finds the intersection of 2 sets (those elements which occur in both the sets), the set on which the method is called and the other set, which is passed as a parameter.
- void UnionWith(Set other) – It finds the union of 2 sets (all elements in both the sets), the set on which the method is called and the other set, which is passed as a parameter.
- bool IsSubsetOf(Set other) – Returns true if the set (on which the method is called) is a subset of the other set passed as a parameter, else returns false
- bool IsSupersetOf(Set other) – Returns true if the set (on which the method is called) is a superset of the other set passed as a parameter, else returns false
- int Count – Returns the number of elements in the set
Following is one of the example code for the set:
using System;
using System.Collections.Generic;
class HS {
// Driver code
public static void Main()
{
// Creating a HashSet of odd numbers
HashSet<int> odd = new HashSet<int>();
// Inserting elements in HashSet for (int i = 0; i < 5; i++) { odd.Add(2 * i + 1);
}
Console.WriteLine("Elements in the HashSet:"); // Displaying the elements in the HashSet
foreach(int i in odd)
{
Console.Write(i+" ");
}
Console.WriteLine("\nTotal elements in the HashSet = "+odd.Count);
Console.WriteLine("Is it possible to remove 7? : "+odd.Remove(7));
Console.WriteLine("New HashSet = ");
foreach(int i in odd)
{
Console.Write(i+" ");
}
}
}
Output:
Comparison Table
The following table illustrates the difference between list and set:
List | Set |
1. Can contain duplicate elements | 1. Elements must be unique |
2. Order of elements is important | 2. Order of elements is not important, but it depends on the implementation |
3. Elements are accessed using index | 3. Elements themselves are indices |
4. The interface used to implement the list is System.Collections.IList | 4. The interface used to implement the set is System.Collections.ISet |
5. The list is implemented as a static list (using array) and dynamic list (linked list) | 5. Sets are implemented as hashset (hashtable) and sorted set (red-black tree-based)
|
Recommended Articles
This is a guide to List vs Set. Here we have discussed the List vs Set key differences with infographics and comparison table. You may also have a look at the following articles to learn more –